5
votes
1answer
594 views

Why do I have to escape a “dot” twice?

I know that we can escape a special character like *(){}$ with \ so as to be considered literals. For example \* or \$ But in case of . I have to do it twice, like \\. otherwise it is considered ...
1
vote
2answers
45 views

Using exec in find over ssh from shell script

So, I am attempting to execute the following from within a shell script; ssh -q $CUR_HOST "cd $LOGS_DIR; echo cd $LOGS_DIR; find . -name *.log -mmin +1440 -exec gzip {} \; exit" When this runs, it ...
1
vote
2answers
36 views

Problems with basename in a loop

I am new at shell script programming and I'm trying to execute a software that reads a text and perform it's POS tagging. It requires an input and an output, as can be seen in the execute example: $ ...
0
votes
1answer
53 views

How to exit the script in ssh session?

My script contains a ssh command to login to remote server and execute a few commands, The command checks if a file exist and if not it should come out of the entire script. ssh $user@$hostname "if[ ...
2
votes
1answer
71 views

Why is my tar in a script behaving different than using tar manually

I'm writing a backup script for my email and cloud service. For simplicity, I only posted the mail-backup part here. Basically, I have a Host-Machine with some Virtual Boxes running in it. These ...
1
vote
1answer
61 views

grep and command-line variable substitution

In the following command sequence, $ grep -E '^.{11,22}John' emplist Operations John Thompson PARTHawks Cher Sales John Jacobs FULLHawks Davinder Singh Finance ...
18
votes
2answers
641 views

Why does my shell script choke on whitespace or other special characters?

Or, an introductory guide to robust filename handling and other string passing in shell scripts. I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some ...
1
vote
1answer
36 views

Problem in using user defined variable in a script

The following is a simple script, named myscript, that I wrote in order to run a command. dcmpath='$HOME/Data' dcmfile='IM1' dcm2nii $dcmpath/$dcmfile Unfortunately, bash does not recognize the ...
1
vote
2answers
564 views

How can I run a “grep | grep” command as a string in a bash function?

I'm trying to build a command that pipes the results of one grep command to another grep command in a bash function. Ultimately, I want the command executed to look like this: grep -I -r FooBar ...
4
votes
2answers
424 views

Bash command in string is executing when I create the string, not when I use it later

I'm relatively new to shell scripting, but have nearly completed a script which makes use of the lftp program. The part of the script I am having trouble with is when I create a long string of ...
0
votes
1answer
66 views

Scripting - Folders with spaces

I'm trying to write a script that's robust enough to work with spaces. Its currently in a for loop, but the problem I'm having is folders with spaces in its name. ASOURCE="Home Photos 2012 ...
1
vote
1answer
105 views

For loop not getting executed correctly over ssh

I have a script in which I am reading from a csv file where it contains SourceIp, DestinationIP,Source Ports, Destination Ports. First I am reading the sourceIp and trying to do ssh over it (I am ...
1
vote
2answers
54 views

Bash script is not working

I wanted to create a script that executes the ls command with the provided arguments. Why isn't it working? I tried running it with for example -R, then it echoes (I echoed the same thing for ...
2
votes
5answers
309 views

Quoting and escaping

I have a shell script, the arguments of which are collected in quote as a single argument and passed to a perl script. /usr/local/API/check_api.sh "-D x.x.x.x -C Test_Internal_Cluster -u user -p pass ...
2
votes
2answers
453 views

Do I need to encapsulate awk variables in quotes in order to sanitize them?

As per an answer on stackoverflow, it's my understanding that encapsulating bash variables in double-quotes is a fairly safe way of sanitizing user input. What about awk variables? For example, if ...
4
votes
3answers
339 views

Help Shell script to pass variables to rsync

I'm trying to create a simple wrapper shell script for rsync. When I send file names to my script, rsync can never seem to identify the correct location. The file names have spaces in them. I've tried ...
4
votes
2answers
125 views

Expansion of a shell variable and effect of glob and split on it

This post actually contains two separate questions but I think grouping them together will give some more context. I have gone through this question on quotes around variables but I do not quite ...
1
vote
1answer
32 views

Problematic quotations

#!/bin/ksh typeset FILE_SIZE1=$(stat -f '%z' test.txt); typeset -r BK_STATARGS="-f '%z'" typeset FILE_SIZE2=$(stat ${BK_STATARGS} test.txt); echo without arg var ${FILE_SIZE1} echo with arg var... ...
5
votes
3answers
86 views

how do I set quotes around a variable so that the programs sees them as quote marks

I am trying to get quotes around a variable to make is just like I typed it in the terminal to get this script to work. it shows " quotes around the varibale " but still does not see it as quotes, as ...
8
votes
3answers
118 views

POSIX compliant way to work with a list of filenames possibly with whitespace

I have seen Bash scripting guides suggesting the use of array for working with filenames containing whitespace. DashAsBinSh however suggests that arrays are not portable so I am looking for a POSIX ...
2
votes
1answer
129 views

Can't echo or create a symbolic link to a file with special characters in its name inside bash script

I'm trying to create a symbolic link to files with the following naming convention: .$abc$namehere.zip;1 As can be seen, it has the following special characters in it: . $ ; In my bash script I ...
5
votes
1answer
206 views

Variable in sed

I have a file that contains filenames with absolute path. In the middle of that path, is a variable directory, whenever I hard code the variable part, it works fine, but when I replace it with the ...
9
votes
2answers
363 views

Are quotes needed for local variable assignment?

Can I safely omit quotes on the right side of a local assignment? function foo { local myvar=${bar} stuff() } I'm mainly interested in bash, but any info on corner cases in other shells are ...
0
votes
2answers
208 views

Making md5sum understand file names with spaces

I need to use md5sum in Python by using pipe to calculate checksum for a bunch of .mp3 files... is there a command which ignore whitespaces in filenames on the command line of md5sum program? For ...
1
vote
1answer
133 views

What is the difference between ' and "? [duplicate]

I tried to create a text file in a bash script. The echo command spans multiple lines and has some double quotes "". #!/bin/bash echo "blabla bla bla bla "blabla" bla bla " bla bla bla" and so on ...
2
votes
2answers
110 views

Awk not rendering any result

My goal is to get the file created in the current month in a directory. It seems that the command is correct but not rendering any result: Date=`date '+%b'` echo $Date Oct ls -l | awk -v d="$Date" ...
12
votes
0answers
353 views

Should I use $* or $@? [duplicate]

Which one of $* or $@ is better to use (in array syntax,command line parameters, etc.) to avoid any bugs/problems in code? Or it does not make any difference?
2
votes
1answer
83 views

Why does backslash get converted to space in echo statement if IFS='\'?

Why is \ is converted to space on setting IFS='\' var="'(]\\{}\$\"" IFS='\' echo $var # '(] {}$"
0
votes
1answer
1k views

Why does scp say “No such file”?

I have a bash script that uses scp to copy a file from my machine to another. The script keeps on exiting after I enter the SSH password with the error: <filename>: No such file or directory ...
0
votes
2answers
260 views

sed with auto escape character [duplicate]

CommentAppend() { # Comment line and append line below commented line local comment="$1" # search this line and comment it local append="$2" # Append this line below commented ...
2
votes
3answers
755 views

Using a reference to a bash string variable in sed

I'm a relative Linux novice. Suppose that I have a text file a.txt that contains the following text: A B C Now, if I want to change the text on line 2 (which contains B), I can use the following ...
3
votes
3answers
462 views

Quote a variable in shell script “if” statement

e.g. if [ "$FOO" = "true" ]; then vs if [ $FOO = "true" ]; then What is the different? Seems both of two statements also works.
2
votes
2answers
2k views

Passing a variable to sed

I cannot not use a shell variable in sed in the $NUMBER form. I have this line in my shell script: cat shared.txt sed 's/whatever1/$2 ... whatever2/' > shared2.txt The result in shared2.txt ...
1
vote
1answer
119 views

Prompt user for sentence and store it without being disturbed by spaces

I have a bash function that prompts user to input certain data. I tried it with first name, last name, birthday etc. Data that is mostly represented in one sentence. When I ask for street + no. the ...
0
votes
1answer
153 views

Unexpected whitespace using sqsh in command substitution [closed]

Below shell script instruction behave in weird way ARG_DATE=`sqsh -S $SERVER -U $DB_USER -P $DB_PASSWORD -D dbname -h<<END SET NOCOUNT ON go select convert(varchar, PRIOR_COB_DATE, 112) from ...
2
votes
3answers
467 views

Very strange behavior with grep and IFS

I'm having trouble using grep, the returned results are "n-empty", I mean without the 'n' character... This is the script sample : OLDIFS=$IFS IFS="\\n" i=$(grep -ril $1 *) echo $i IFS=$OLDIFS ...
4
votes
5answers
271 views

How to use ' in alias?

I have one-line that I want to call using alias. while printf '%s ' "$(df -P / | awk 'NR==2 { print $(NF-1) }')"; do sleep 30; done I tried to escape ' like \' but it didn't work. What is the ...
12
votes
1answer
795 views

When is double-quoting necessary?

The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of ...
2
votes
2answers
327 views

How to pass the output of a script to a command like ls without the output being split?

Suppose I have a script like this: #!/bin/bash printf '%q\n' "b c" Executing the script prints: b\ c on the commandline. Now, being in a directory which contains a file named b c I want to pass ...
3
votes
3answers
193 views

Zsh function with su and echo

I'm trying to add a function to my .zshrc that makes adding new USE flags to my /etc/portage/package.use file easier. Normally, I'd have to do su -c 'echo "net-misc/aria2 bash-completion bittorrent" ...
1
vote
0answers
83 views

How to make tcsh not insert whitespace in a command that spans multiple lines

Bash does not seem to insert whitespace but tcsh does. Tcsh bash-3.2.25$ tcsh -c 'echo $tcsh; echo A\ > B' 6.14.00 A B Bash bash-3.2.25$ bash -c 'echo A\ > B' AB How can I ask tcsh to not insert ...
3
votes
1answer
460 views

Printing colored text using script

When I type below command in the shell I get the OUTPUT in green color. Command echo "\033[32mCONNECTING TO abpwrk\033[m"; Output(in green color) CONNECTING TO abpwrk But if I use the same ...
1
vote
2answers
154 views

run bash in script

I want to creat a script to run an another script with a several parameter exp=([1]=bloc [2]=ins [3]=rep [4]=op) for j in ${!exp[*]} do arr=([1]=mem [2]=gen [3]=usr) for i in ${!arr[*]} ...
19
votes
3answers
6k views

What does it mean to have a $“dollarsign-prefixed string” in a script?

I just saw this in an init script: echo $"Stopping Apache" What is that dollar-sign for? My research so far: I found this in the bash manual: extquote If set, $'string' and ...
2
votes
1answer
357 views

Command substitution: file or command not found

The following script #!/bin/bash QUERY='select * from cdr;' MYROWS=$("sqlite3 -list -nullvalue NULL -separator ',' /var/log/asterisk/master.db '${QUERY}'") gives me ./bla.sh: row 35: sqlite3 -list ...
20
votes
2answers
737 views

Why escape trivial characters in shell script?

I just opened a legacy shell script (written in old ksh88 on Solaris) and found the following repeated all throughout the code: [ -f $myfile ] && \rm -f $myfile The escaping backslash ...
2
votes
2answers
382 views

Shell script output incorrectly splitting while being passed as an argument to script

Let's say I have the following two shell scripts: #!/bin/sh #This script is named: args.sh echo 1 "\"Two words\"" 3 , and: #!/bin/sh #This script is named: test.sh echo "Argument 1: "$1 echo ...
17
votes
3answers
37k views

What characters do I need to escape when using sed in a sh script?

Take the following script: #!/bin/sh sed 's/(127\.0\.1\.1)\s/\1/' [some file] If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't ...
0
votes
1answer
403 views

Shell script to check server state

I am writing a script that would ssh into the VMWare server and check if the VMs are up or not. There's some error in the server=awk "/$i/{print $2}" /home/dharmit/scripts/vmids line. I am getting ...
5
votes
1answer
4k views

Bash: Merge foldername from variable with filename

First I write a configfile with all my parameters like this path="/home/test/" I name it test.conf. Then I write a shell script with this content, name it test, and make it executable with chmod ...