Bash Script Loop Question
I don't have a lot of experience scripting in Bash, but am working on a script right now. What the script is doing is comparing identically named files in two different directories, then reporting the differences between them with some information parsed out of the file and filename.
Here is the script: Code:
#!/bin/bash The script is running to completion, but is throwing the error: ./wfCompare.sh: 31: [0]: not found Line 31 is the 'done' closing the for loop. It throws it on every iteration. It also gets thrown once at the end on line 39, which is the 'fi' closing the final if statement. Any idea what this means? |
Code:
if [$changeFlag] ; then Code:
if [ $changeFlag -eq 0 ] ; then |
As colucix pointed out, your main problem is in your test statement. You have to put spaces around the test brackets. This is because '[' is actually a command name. It's an alias for 'test'. The ending bracket is not strictly necessary, but all modern bash versions require it.
But let's give you a few other formatting tips also. 1) The double quotes (") are considered the "weak" quoting mechanism, because variable expansion and some other translations will still work inside them. This means you can do, for example: Code:
echo "Comparing $file -- $changeFlag" Single quotes, OTOH, (') are "hard" quotes, because everything inside them will be taken literally. 2) The backtick (`) form of command substitution has been superseded by the '$()' form. Not only is it easier to read, but they can more easily be nested. Code:
wfName=`echo $wf_twist_dir | sed 's/.*\/\(.*$\)/\1/'` Finally, even the sed command can be cleaned up if you use some other character as the separator instead of '/', and add the -r (extended regex) flag, so you don't have to escape everything. Code:
wfName="$(basename $wf_twist_dir)" 4) '$variable' is only used when you want to output the value of a variable. Use the variable name without the dollar sign when you want to set it. You made this mistake inside the if statement. Also, '${variable}' is the extended form of '$variable', which will let you more safely combine them inside of character strings, as well as performing various parameter substitutions. You should generally quote the variable values when you set them (variable="value"), except perhaps when it's a simple integer or such. Code:
changesExist=1 5) Your final echo statement will not be formatted correctly. You need to use 'echo -e' for it to translate the newline characters in the variable output. Also the variable should be quoted so that the contents are seen as a single unit. Code:
echo -e "$emailBody1" |
Quote:
|
Quote:
|
Thanks for the tips, guys. Much appreciated.
|
All times are GMT -5. The time now is 05:11 PM. |