I am developing simple shell script which copy all my present directory files to backup directory which will be exist in present working directory. now i'm getting error when i pass more then one condition in if.
#!/bin/bash
filename=nx.pdf
for i in *;
do
echo $i;
if [ $i == backup || $i == $filename ] ; then
echo "Found backup."
else
echo "Part 2"
cp -rf $i backup
fi
done
I am getting error
asd.sh: line 6: [: missing `]'
asd.sh: line 6: ==: command not found
Part 2
deployee.sh
asd.sh: line 6: [: missing `]'
asd.sh: line 6: ==: command not found
Part 2
The compare operator is = (as defined in POSIX). But == works on some shells as well.
Something like this should work:
if [ $i = backup ] || [ $i = $filename ] ; then
You should quote $i in "". Otherwise you get syntax errors for filenames with blanks.
To be able to use || and && in conditions, you have to use the double square brackets:
if [[ $i == backup || $i == $filename ]] ; then
Related
This question already has answers here:
How to do a logical OR operation for integer comparison in shell scripting?
(8 answers)
Closed 3 years ago.
I am writing a bash script that cycles through a list of files and does something either when the file contains a certain word, or the file was created within the last X seconds. The script works fine with only the first requirement
if grep -q $error $f; then
, but I don't know how to include the "or" statement.
I have tried the following ( I am new to bash so I tried things that may not even make sense ).
FILES=logdir/*.out
OLDTIME=86400
CURTIME=$(date +%s)
error='exit'
for f in $FILES
do
FILETIME=$(stat $f -c %Y)
TIMEDIFF=$(expr $CURTIME - $FILETIME)
if [[ grep -q $error $f ]] || [[$OLDTIME -gt $TIMEDIFF ]] ; then
or
if [[ grep -q $error $f || $OLDTIME -gt $TIMEDIFF ]] ; then
gives me
./script.sh: line 12: conditional binary operator expected
./script.sh: line 12: syntax error near `-q'
and
if [ grep -q $error $f ] || [ $OLDTIME -gt $TIMEDIFF ] ; then
gives me
./script.sh: line 12: [: too many arguments
./script.sh: line 12: [: 1: unary operator expected
How can I have both conditions in the if statement?
It's easier than you think :-)
if /bin/true || /bin/false; then echo hi; fi
prints hi
if /bin/false || /bin/false; then echo hi; fi
prints nothing.
In other words, you don't need the [ or [[ operators. Just execute the commands to return their status and use || to "or" them.
#!/bin/bash
export folder=`date -d "today - 1 days" '+%Y%m%d'`;
if filename in /r1/test/med_sms/FDA3A; then
result=
if filename = *$folder*; then
result= mv $filename /r1/test/med_sms/FDA3A/test
else
if filename = *$folder*; then
result= mv $filename /r1/test/med_sms/FDA3A/test
elif
done;
Hi..I'm trying to execute the above scrpot but it is throwing the exception as ./test1.sh: line 11: syntax error near unexpected token `done'
./test1.sh: line 11: `done'
Can someone please help on this?
$ help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]...
[ else COMMANDS; ] fi
Bash if statements end in fi
You shouldn't be using the ìf elif statement that way, according to the structure of your program this should be looking something like :
export folder=`date -d "today - 1 days" '+%Y%m%d'`;
if filename in /r1/test/med_sms/FDA3A; then
result= something_here
elif filename = *$folder*; then
result= mv $filename /r1/test/med_sms/FDA3A/test
elif filename = *$folder*; then
result= mv $filename /r1/test/med_sms/FDA3A/test
else
result = PUT here the last condition
fi
anothervar = 1
while [$anothervar -lt 1 ] do
read a
if [ 42 = $a ]; then
$anothervar = 2
else
echo $a
fi
done
get line 9: syntax error near unexpected token `done' error.
What did i do wrong ?
If you paste your shell script into ShellCheck you will see the following two shell script analysis messages for line 2 of your shell script:
You need a space after the [ and before the ].
Use semicolon or linefeed before 'do' (or quote to make it literal).
Your shell script after making the two corrections to line 2 suggested by the automated shell script analysis and changing the first line to anothervar=0 so that the commands inside the while loop can be executed is:
anothervar=0
while [ $anothervar -lt 1 ]; do # fixes 2 errors in this line
read a
if [ 42 = $a ]; then
anothervar=2
else
echo $a
fi
done
Alternatively:
anothervar=0
while [[ $anothervar -lt 1 ]]
do
read a
if [[ 42 = $a ]]
then
anothervar=2
else
echo $a
fi
done
Voila, no semicolons, and spaces in the variables don't bother you anymore. ;-)
I have the following bash script:
#!/bin/bash
if [ ! -f numbers ]; then echo 0 > numbers; fi
count = 0
while [[$count != 100]]; do
count = `expr $count + 1`
done
When I run it in terminal on my Mac, I get the following output:
seq_file_gen.sh: line 3: count: command not found
seq_file_gen.sh: line 4: [[: command not found
Why am I getting these errors? This script was given to me by my teacher so I have no idea why I can't get this script to run. Any help would be greatly appreciated.
EDIT:
This is the correct way to write this script (with spaces)
#!/bin/bash
if [ ! -f numbers ]; then echo 0 > numbers; fi
count=0
while [[ $count != 100 ]]; do
count=`expr $count + 1`
done
Add spaces before/after [[ and ]] like so:
while [[ $count != 100 ]]; do
I have looked at this for about 30 minutes now and can't seem to find the error in this. It happens at my if/else block at the end.
default()
{
for file in /*
do
if [ -f $file ]; then
((filecount++))
elif [ -d $file ]; then
((dircount++))
fi
done
echo The number of files is "$filecount"
echo The number of directories is "$dircount"
}
specific()
{
for file in $param
do
if [ -f $file ]; then
((filecount++))
elif [ -d $file ]; then
((dircount++))
fi
done
echo The number of files is "$filecount"
echo The number of directories is "$dircount"
}
#Variables
declare -a param=$1
declare -i filecount="0"
declare -i dircount="0"
#Action
if [ $param=='-h' ]; then
echo To use this program, enter a directory path after $0 or leave it blank to use current directory.
elif [ $param=='' ]; then
default()
else
specific()
fi
exit 0
Here is the error code. Any help is appreciated.
./countf.sh: line 44: syntax error near unexpected token `else'
./countf.sh: line 44: `else'
I checked your syntax only and found these errors.
function calls. As #Etan Reisner mentioned.
You need spaces around the comparison operator. like [ $param == '-h' ];
you need to double quote your variable. use [ "$param" == '-h' ] instead of [ $param == '-h' ] . Check this for more details. Double quote to prevent globbing and word splitting
I suggest you to test your script here. http://www.shellcheck.net/ I also should do that first instead of manually check your script.