Bash file existence checking missing ] [closed] - linux

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am attempting a simple file existence check in my bash script:
FILE_TO_CHECK=/home/username/path/to/file
if [ ! -f $FILE_TO_CHECK]; #line 9 in actual script
then
echo File not found.
fi
Seems simple enough to me, but I'm getting the following error and I don't know why:
/path/to/script: line 9: [: missing `]'
I'm not exactly a bash expert, but I was pretty sure a backtick is not necessary in this context. What gives?

Missing space before the closing ].
You have to understand that [ is a command and everything following it, until the ;, are its arguments. Command [ expects its last argument to be ]. But if you omit the space, then ] becomes the last character of the previous argument.
It might seem that [ ] is part of the if syntax. It's not the case. if has to be followed by any command, and if evaluates its exit status.
if true; then foo; fi
In the above line true is a command too.
$ which true
/bin/true
true is a command with the sole purpose of always having a true (0) exit status.

You could also try:
if (test ! -f $FILE_TO_CHECK);
or
if !(test -f $FILE_TO_CHECK);
as [ is a shorthand for the test command.

Related

AWK command not working in linux but works in mac [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Can someone tell me what am I doing wrong here? It seems to work on my mac shell but does not work on linux box it seems. Looks like different version of awk? I want to make sure my code works on the linux version.
echo -e "${group_values_with_counts}" | awk '$1>='${value2}' { print "{\"count\":\""$1"\",\"type\":\""$2"\"}" }'
21:19:41 awk: $1>= { print "{\"count\":\""$1"\",\"type\":\""$2"\"}" }
21:19:41 awk: ^ syntax error
You're trying to pass the value of a shell variable into awk the wrong way and using a non-portable echo. The right way (assuming value2 doesn't contain any backslashes) is:
printf '%s\n' "$group_values_with_counts" |
awk -v value2="$value2" '$1>=value2{ print "{\"count\":\""$1"\",\"type\":\""$2"\"}" }'
If value2 can contains backslashes and you want them treated literally (e.g. you do not want \t converted to a tab character) then you need to pass it in using ENVIRON or ARGV. See http://cfajohnson.com/shell/cus-faq-2.html#Q24.

cmp in if statement (Bash) [duplicate]

This question already has answers here:
if fi [: too many arguments
(3 answers)
Closed 5 years ago.
if [ cmp -s "$expectedOut" "$actualOut" ]; then
The following line of code keeps giving me errors saying that there are too many arguments. however I know this is the proper typical use of cmp so I think it may have to do with the brackets. Anyone know whats really going on here?
Lose the [ ].
if cmp -s "$expectedOut" "$actualOut" ; then
The syntax of if is
if Command; then
[ is just one possible command (on that happens to expect ] as its last argument to make things look pretty).

Getting error with if condition [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What is wrong with my below script?
I am new to scripting and trying out simple scripts. I am getting an error with my script.
START_OUT = `grep -c "Start Report" jeevagan/test_scripts/log.txt`
FINISH_OUT = `grep -c "Finished Report" jeevagan/test_scripts/log.txt`
if [$START_OUT == $FINISH_OUT]
then
echo "All good"
else
echo "Warning!!!Monitor Logs"
fi
bash/sh is very space sensitive. You want
if [ $START_OUT == $FINISH_OUT ]
Note the spacing around the brackets. "=" may be used instead of "==" for strict POSIX compliance. See here for more details and note the comment
In a script, the different parts of the if statement are usually
well-separated.
try leaving spaces on the if statement and add quotes on the variables, like this
if [ "$START_OUT" == "$FINISH_OUT" ]
If this is still not working try this exactly, this works for sure, if it still gives you an error, then use echo brefore the if statement and check what $START_OUT and $FINISH_OUT variables have stored, because the problem could be there, after you do this give us feedback:
if test "$START_OUT" = "$FINISH_OUT"
okayyyy, this was one of your problems, the other problem is that you left a space when you used grep and tried to add it in the variable...
i will rewrite your code, copy and paste it please. as you had it, it was like you calling the START_OUT command which obviously doesnt exist.... and tell me if it worked.
START_OUT=`grep -c "Start Report" jeevagan/test_scripts/log.txt`
FINISH_OUT=`grep -c "Finished Report" jeevagan/test_scripts/log.txt`
if test "$START_OUT" = "$FINISH_OUT"
then
echo "All good"
else
echo "Warning!!!Monitor Logs"
fi
if [$START_OUT == $FINISH_OUT] is wrong. take care about spaces. it should be like
if [ $START_OUT == $FINISH_OUT ]

Bash if/else error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a script to compile a program in bash and my if/else statement on line 84 is not working. I get this error:
./build2.sh: 84: [: no: unexpected operator
What is the problem?
http://www.pasteall.org/62904/bash
Thanks in advance!
"Is not working" is very faint. However, I'll give it a try...
If enter is pressed without typing any reply, $putProgramOn is empty, and
if [ $putProgramOn == "no" ] resolves to if [ == "no" ], which is a syntax error, so you need at least to quote the reply in the conditional:
if [ "$putProgramOn" == "no" ]
See also Bash Comparison Operators.
BTW: With esac you can simply have shortcuts "y" and "n" evaluated as well:
read -p 'Would you like to put the program on the flash drive? Awnser yes or no: ' putProgramOn
case "$putProgramOn" in
n|no)
echo "putProgramOn=no"
;;
y|yes)
echo "putProgramOn=yes"
;;
*)
echo "putProgramOn=\"putProgramOn\""
exit 1
;;
esac

Shell variable is being treated as command [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have written this small script to compare file name with the files in one folder and copy them to another folder if they do not exist in the first one. Please refer code. But for some reason Ubuntu 15.04 is treating my variable as command and giving me following error:
./COPY_FILES.sh: line 8: FILE_EXIST_IN_SUPER_STRING: command not found
while read NAME1
do
FILE_EXIST_IN_SUPER_STRING = 0
while read NAME2
do
if [ "$NAME1" == "$NAME2" ]
then
FILE_EXIST_IN_SUPER_STRING = 1
fi
done < file_superstring.txt
if [ "$FILE_EXIST_IN_SUPER_STRING" == 0 ]
then
cp Master/"$NAME1" Non-SuperString/"$NAME1"
fi
done < Total_files.txt
Third line should have no spaces.
It should be:
FILE_EXIST_IN_SUPER_STRING=0

Resources