Good morning ! trying to execute this code but i have an error on if statement .
error message : error script_name.sh: line 6: [[0: command not found
the problèm is on "if" statement.
help please
#!/bin/ksh
jour=$(date +%Y%m%d)
#Control if run is ok or not before sending mail
dir_resultFailure=/transfertCLINK/Share/RESULT_UAT/$jour/FichierFailure/
dir_resultFilteredOut=/transfertCLINK/Share/RESULT_UAT/$jour/FichierFilteredOut/
if [[ `ls $dir_resultFailure | wc -l` -eq 0 ]] && [[`ls $dir_resultFilteredOut | wc -l` -eq 0 ]]
then
echo "repo is empty."
fi
You could have it in following way.
#!/bin/ksh
jour=$(date +%Y%m%d)
#Control if run is ok or not before sending mail
dir_resultFailure="/transfertCLINK/Share/RESULT_UAT/$jour/FichierFailure/"
dir_resultFilteredOut="/transfertCLINK/Share/RESULT_UAT/$jour/FichierFilteredOut/"
if [[ $(ls $dir_resultFailure | wc -l) -eq 0 ]] && [[ $(ls $dir_resultFilteredOut | wc -l) -eq 0 ]]
then
echo "repo is empty."
fi
Improvments/Fixes in OP's attempts:
Always wrap your variables values inside ".
Using backticks is deprecated now, use $(....) for saving variables values.
Your if condition was not correct, you should have spaces in between [[ and (.
Related
I'm trying to make a bash script that reads integers from a file (one number per line, name of the file is passed as the script argument), finds maximum, minimum and sum. I've got a problem with the part, where I'm comparing variables, though. Code below (I've skipped here the part which checks whether the file exists or is empty):
#!/bin/bash
min=`cat "$1" | head -n 1`
max=$min
sum=0
lw=`cat "$1" | wc -l`
while [ $lw -gt 0 ];
do
num=`cat "$1" | tail -n $lw | head -n 1`
if [ "$num" -gt "$max" ]
then
max=$num
elif [ "$num" -lt "$min" ]
then
min=$num
fi
sum=$[sum+num]
lw=$[$lw-1]
done
echo "Maximum: $max"
echo "Minimum: $min"
echo "Sum: $sum"
With this code I'm getting errors in lines 13 and 16: [: : integer expression expected
If I change the comparision part inside the while loop to:
if [ $num -gt $max ]
then
max=$num
elif [ $num -lt $min ]
then
min=$num
fi
I'm getting errors:
line 13: [: -gt: unary operator expected
line 16: [: -lt: unary operator expected
What am I doing wrong? I'm a total newbie in bash, so I'll be extremely grateful for any help.
Data that I used for testing:
5
6
8
2
3
5
9
10
Probably your input file contains DOS line endings or other improper formatting. Your code should work for well-formed inputs.
However, the proper way to loop over the lines in a file is
#!/bin/bash
min=$(sed 1q "$1")
max=$min
sum=0
while read -r num; do
if [ "$num" -gt "$max" ]
then
max=$num
elif [ "$num" -lt "$min" ]
then
min=$num
fi
((sum+=num))
done<"$1"
echo "Maximum: $max"
echo "Minimum: $min"
echo "Sum: $sum"
Notice also that backticks and $[[...]]] use syntax which has been obselescent for decades already.
My guess would be that the expression
num=`cat "$1" | tail -n $lw | head -n 1`
assigns to num some value that is not a number in one of the iterations. I would suggest adding echo "$num" in the prev line to check this assumption
Another thing: instead of reading lines using cat | tail | head it is easier to read file line by line using the following syntax
while IFS= read -r line
do
echo "$line"
done < "$input"
This will read contents of input file into line variable.
See here for explanations about IFS= and -r https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/ - both of them not really necessary in your case
I want to check is screen with my server is on the screen list. I use this script but it's sucks and don't work
#!/bin/bash
SERWER="test_start.sh"
SCRN="testMinecraft"
if ! [[ screen -ls | grep $SCRN ]]; then
/root/testser/$SERWER
fi
idk what i need to change there to start this script
EDIT:
I change it to this, but it still doesn't work
#!/bin/bash
SERWER="test_start.sh"
SCRN=`screen -ls | grep "testowyscreen" | wc -l`
if ! [ $screen -eq 1 ]; then
echo "there is nothing";
/root/testser/$SERWER
fi
output: ./test_restart.sh: line 6: [: -eq: unary operator expected
bash supports [[ and ]] and you need to place exclamation mark inside the bracers, like this:
#!/bin/bash
SERWER="test_start.sh"
SCRN=`screen -ls | grep "testowyscreen" | wc -l`
if [[ ! $screen -eq 1 ]]; then
echo "there is nothing";
/root/testser/$SERWER
fi
P.S.
`` is deprecated you should use $()
Consider this simple shell script:
#!/bin/bash
exp1="[ $1 -gt 5 ]";
exp2="[ `ping localhost -c 4 | wc -l` -gt 0 ]";
if eval $exp1 || eval $exp2
then
echo Ok!
fi
I was expecting that passing 10 to this script, the ping was not executed, but it is clearly executed (since the script takes some seconds to write "Ok!").
I was wondering if there is a way to avoid/optimize this behavior?
It will execute ping because you have:
exp2="[ `ping localhost -c 4 | wc -l` -gt 0 ]";
which uses command substitution before evaluating the condition due to double quotes.
Use single quotes to delay the execution until the eval line, like so:
#!/bin/bash
exp1='[[ $1 -gt 5 ]]'
exp2='[[ $(ping localhost -c 4 | wc -l) -gt 0 ]]'
if eval $exp1 || eval $exp2
then
echo Ok!
fi
Better yet, use functions. Don't store commands in variables!
exp1() { [[ $1 -gt 5 ]]; }
exp2() { [[ $(ping localhost -c 4 | wc -l) -gt 0 ]]; }
if exp1 "$1" || exp2
then
echo Ok!
fi
I have the following instruction:
if [[ ! `wc -l <<< $SILENT_LOG` -eq 1 -o ! `wc -l <<< $ACTIVE_LOG` -eq 1 ]] then
echo "There should be only a silent report log file!" >> $DEST_DIR/$RESULT_FILE
OK=0;
fi
OK should be set to 0 when SILENT_LOG or ACTIVE_LOG have 0 or more lines. The problem is that the following error is raised:
**./reportComparator.sh: line 26: syntax error near `-o'
./reportComparator.sh: line 26: `if [[ ! `wc -l <<< $SILENT_LOG` -eq 1 -o ! `wc -l <<< $ACTIVE_LOG` -eq 1 ]] then'**
Also, I tried to replace wc -l <<< $SILENT_LOG with echo $SILENT_LOG | wc-l but it still does not work.
First, the following syntax will not work:
$ [[ 1 -eq 1 -o 2 -eq 2 ]]
bash: syntax error in conditional expression
bash: syntax error near `-o'
In bash, you have two choices:
$ [[ 1 -eq 1 || 2 -eq 2 ]]
$ [ 1 -eq 1 -o 2 -eq 2 ]
For compatibility with plain POSIX shells, neither of the above should be used, the former because plain POSIX does not support [[ and the latter because plain POSIX shells cannot parse such long tests reliably. Instead use:
$ [ 1 -eq 1 ] || [ 2 -eq 2 ]
For the second issue, let's create a variable with three lines:
$ silent_log='one
> two
> three'
Now observe:
$ wc -l <<<$silent_log
1
$ wc -l <<<"$silent_log"
3
If you want the lines counted correctly, you need to double-quote your variables.
You have some syntax issues in your code.
For [[...] you can use:
if [[ $(wc -l < "$SILENT_LOG") -eq 1 || $(wc -l < "$ACTIVE_LOG") -eq 1 ]]; then
echo "There should be only a silent report log file!" >> $DEST_DIR/$RESULT_FILE
OK=0
fi
I'm trying to write a bash script and I needed to do some floating point math. Basically I want to do something like this:
NUM=$(echo "scale=25;$1/10" | bc)
if [ $? -ne 0 ]
then
echo bad
fi
The problem I'm running into is $? tends to hold the output from the echo program and not the bc call. Is there a way I save the output from the bc program into a variable?
EDIT:
Thanks for the quick replies. Here's another way of looking at the problem. Say I modified the script a little bit so it looks like this:
#!/bin/bash
NUM=$(echo "scale=25;$1/10" | bc)
if [ $? -ne 0 ]
then
echo bad
exit
fi
echo "$NUM"
When the user inputs a normal floating point value, it works fine:
bash script.sh 1.0
output:
.1000000000000000000000000
However, when the user enters an incorrect value, the script can't recover:
bash script.sh 1.0a
output:
(standard_in) 1: parse error
What I'm trying to do is get it to exit gracefully.
I don't see anything wrong. $NUM is supposed to hold your bc command results
see:
NUM=$(echo "scale=25;$1/10" | bc)
echo "\$? is $?"
echo "NUM is $NUM"
output
$ ./shell.sh 10
$? is 0
NUM is 1.0000000000000000000000000
another way is to use awk
NUM=$(awk -vinput="$1" 'BEGIN{printf "%.25f", input/10 }')
echo "\$? is $?"
echo "NUM is $NUM"
The other way, is to do the check of "$1" before you pass to bc. eg
shopt -s extglob
input="$1"
case "$input" in
+([0-9.]))
IFS="."; set -- $input
if [ $# -ne 2 ];then
echo "bad decimal"
else
NUM=$(echo "scale=25;$1/10" | bc )
echo "$NUM"
fi
esac
you don't have to check for $? from bc anymore
For GNU bc, an error similar to "(standard_in) 1: syntax error" will be output on stderr. You can capture this in your variable and check for it.
#!/bin/bash
NUM=$(echo "scale=25;$1/10" | bc 2>&1)
if [[ $NUM =~ error || $? -ne 0 ]]
then
echo bad
exit
fi
echo "$NUM"
Are you after the result of calculation from bc (which you store in NUM) or the status return from the system call?
As I said you have the result of calculation in $NUM:
#bctest.sh
NUM=$(echo "scale=25;$1/10" | bc)
if [ $? -ne 0 ]
then
echo bad
fi
echo "result: ", $NUM
Test:
bash ./bctest.sh 15
result: , 1.5000000000000000000000000