I have this code here.
But I get the error:
./concatconvert: line 9: [: -eq: unary operator expected
./concatconvert: line 18: [: -eq: unary operator expected
This is my code:
#!/bin/bash
param=$#
if [ $# -le 2 ]
then
echo "Usage: concatconvert [-u|-1] FILE ...
Description: concatenates FILE(s) to standard output separating them with divider -----. Optional first argument -u or -l converts contents to uppercase or lowercase,$
fi
if [ $1 -eq "-u" ]
then
while [ $param -ge 1 ]
do
./concat | awk '{print toupper($0)}'
param=$(( param-1 ))
shift
done
fi
if [ $1 -eq "-l" ]
then
while [ $param -ge 1 ]
do
./concat | awk '{print tolower($0)}'
param=$(( param-1 ))
shift
done
fi
Why am I getting this error? I thought that -eq is a unary operator?
You have missed few things eg--> " for echo command was NOT closed. Then in if condition since you are comparing a string so change it to if [[ "$1" = "-u" ]] so following could be the script(I haven't tested it since no samples were there).
#!/bin/bash
param=$#
if [ $# -le 2 ]
then
echo "Usage: concatconvert [-u|-1] FILE ...
Description: concatenates FILE(s) to standard output separating them with divider -----. Optional first argument -u or -l converts contents to uppercase or lowercase,$"
fi
if [[ "$1" = "-u" ]]
then
while [ $param -ge 1 ]
do
./concat | awk '{print toupper($0)}'
param=$(( param-1 ))
shift
done
fi
if [[ $1 -eq -l ]]
then
while [ $param -ge 1 ]
do
./concat | awk '{print tolower($0)}'
param=$(( param-1 ))
shift
done
fi
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'm getting error for line 8,11,14,17 .
the program is
#!/bin/sh
read a
b=$(grep -i $a TeamScore.txt| awk 'BEGIN{FS =" "}{print ($2+$4)/2}')
echo "the avg is $b"
if [ "$b" -ge 80] #line8
then
echo "1st class "
elif [ "$b" -lt 80 ] || [ "$b" -ge 70 ] #line11
then
echo "2nd class"
elif [ "$b" -lt 70 ] || [ "$b" -ge 60 ] #line14
then
echo "3rd class"
elif [ "$b" -lt 60 ] #line17
then
echo "fail"
else
echo "code not working"
fi
I'm new to unix and shell scripting and i would like to whats wrong and how can i fix it.the TeamScore.txt contains a table of names and their score(for 2 exams).
The problem is that the test command only understands integers, but you're setting b to 55.5, which contains a fraction. Change the awk command to round it down.
b=$(grep -i $a TeamScore.txt| awk 'BEGIN{FS =" "}{print int(($2+$4)/2)}')
You also need a space before ] on line #8.
Line 8: add an extra space:
if [ "$b" -ge 80 SPACE_HERE_REQUIRED ]
Line11, line14 and line17: number operators can't be compared with strings, so use $b instead of "$b", so, for example, use this
if [ $b -ge 80 ]
instead of:
if [ "$b" -ge 80 ]
COUNTER=0
let COUNTER=COUNTER+1
count=`ssh -i /var/www/.ssh/id_rsa_root -o stricthostkeychecking=no $host $cmd`
count1=`echo $count | awk '{print $4}'`
printf "count1 : $count1\n"
result1=${count1/.*}
if [ "$result1" -ge "0" ]; then
echo $host
else
echo $host
exit
fi
If the value of $result1 is INTEGER and greater than zero, it'll goto IF loop (works fine for me)
But when it is not INTEGER, it is coming to else loop (which it is suppose to do) with the following error in the Output
line 55: [: : integer expression expected
but i dont want the above error in my output. I tried to use 2>/dev/null with this but no luck.
please help!
If you want to handle an empty result gracefully, check for it explicitly:
if [ -z "$result1" ]; then
: "ignoring empty string"
elif [ "$result1" -ge 0 ]; then
printf '%s\n' "$host"
else
printf '%s\n' "$host"
exit
fi
You could also check if result1 is a valid integer before making arithmetic comparisons:
function isNumber () {
[[ $1 =~ ^-?[0-9]+$ ]]
}
if ! isNumber "$result1"; then
echo "not a number"
elif [ "$result1" -ge "0" ]; then
echo "null or positive"
else
echo "negative"
fi
Change if [ "$result1" -ge "0" ]; then to
if (( result1 >= 0 )); then
This syntax won't throw any errors if result1 isn't defined (or empty) or happen to be a string somehow.
I am trying to do a simple condition check, but it doesn't seem to work.
If $# is equal to 0 or is greater than 1 then say hello.
I have tried the following syntax with no success:
if [ "$#" == 0 -o "$#" > 1 ] ; then
echo "hello"
fi
if [ "$#" == 0 ] || [ "$#" > 1 ] ; then
echo "hello"
fi
This should work:
#!/bin/bash
if [ "$#" -eq 0 ] || [ "$#" -gt 1 ] ; then
echo "hello"
fi
I'm not sure if this is different in other shells but if you wish to use <, >, you need to put them inside double parenthesis like so:
if (("$#" > 1))
...
This code works for me:
#!/bin/sh
argc=$#
echo $argc
if [ $argc -eq 0 -o $argc -eq 1 ]; then
echo "foo"
else
echo "bar"
fi
I don't think sh supports "==". Use "=" to compare strings and -eq to compare ints.
man test
for more details.
If you are using the bash exit code status $? as variable, it's better to do this:
if [ $? -eq 4 -o $? -eq 8 ] ; then
echo "..."
fi
Because if you do:
if [ $? -eq 4 ] || [ $? -eq 8 ] ; then
The left part of the OR alters the $? variable, so the right part of the OR doesn't have the original $? value.
Sometimes you need to use double brackets, otherwise you get an error like too many arguments
if [[ $OUTMERGE == *"fatal"* ]] || [[ $OUTMERGE == *"Aborting"* ]]
then
fi
If a bash script
If [[ $input -gt number || $input -lt number ]]
then
echo .........
else
echo .........
fi
exit
have you tried something like this:
if [ $# -eq 0 ] || [ $# -gt 1 ]
then
echo "$#"
fi
From Bash Reference Manual → 3.4.2 Special Parameters
#
($#) Expands to the number of positional parameters in decimal.
Therefore, $# will always be either 0 or a bigger integer.
So if you want to do something whenever $# is either 0 or bigger than 1, you just have to check if $# is or is not 1:
[ $# -eq 1 ] && echo "1 positional param" || echo "0 or more than 1"
This uses the syntax:
[ condition ] && {things if true} || {things if false}
And in Bash
line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1`
vpid=`ps -ef| grep wowzaDataSync | grep -v grep | awk '{print $2}'`
echo "-------->"${line1}
if [ -z $line1 ] && [ ! -z $vpid ]
then
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Process Is Working Fine"
else
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Prcess Hanging Due To Exception With PID :"${pid}
fi
OR in Bash
line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1`
vpid=`ps -ef| grep wowzaDataSync | grep -v grep | awk '{print $2}'`
echo "-------->"${line1}
if [ -z $line1 ] || [ ! -z $vpid ]
then
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Process Is Working Fine"
else
echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` ::
"Prcess Hanging Due To Exception With PID :"${pid}
fi
in bash I need to compare two float numbers, one which I define in the script and the other read as paramter, for that I do:
if [[ $aff -gt 0 ]]
then
a=b
echo "xxx "$aff
#echo $CX $CY $CZ $aff
fi
but I get the error:
[[: -309.585300: syntax error: invalid arithmetic operator (error token is ".585300")
What is wrong?
Thanks
Using bc instead of awk:
float1='0.43255'
float2='0.801222'
if [[ $(echo "if (${float1} > ${float2}) 1 else 0" | bc) -eq 1 ]]; then
echo "${float1} > ${float2}"
else
echo "${float1} <= ${float2}"
fi
use awk
#!/bin/bash
num1=0.3
num2=0.2
if [ -n "$num1" -a -n "$num2" ];then
result=$(awk -vn1="$num1" -vn2="$num2" 'BEGIN{print (n1>n2)?1:0 }')
echo $result
if [ "$result" -eq 1 ];then
echo "$num1 greater than $num2"
fi
fi
Both test (which is usually linked to as [)and the bash-builtin equivalent only support integer numbers.
Use bc to check the math
a="1.21231"
b="2.22454"
c=$(echo "$a < $b" | bc)
if [ $c = '1' ]; then
echo 'a is smaller than b'
else
echo 'a is larger than b'
fi
I would use awk for that:
e=2.718281828459045
pi=3.141592653589793
if [ "yes" = "$(echo | awk "($e <= $pi) { print \"yes\"; }")" ]; then
echo "lessthanorequal"
else
echo "larger"
fi
The simplest solution is this:
f1=0.45
f2=0.33
if [[ $f1 > $f2 ]] ; then echo "f1 is greater then f2"; fi
which (on OSX) outputs:
f1 is greater then f2
Here's another example combining floating point and integer arithmetic (you need the great little perl script calc.pl that you can download from here):
dateDiff=1.9864
nObs=3
i=1
while [[ $dateDiff > 0 ]] && [ $i -le $nObs ]
do
echo "$dateDiff > 0"
dateDiff=`calc.pl $dateDiff-0.224`
i=$((i+1))
done
Which outputs
1.9864 > 0
1.7624 > 0
1.5384 > 0