My current script does the following;
It takes integer as a command line argument and starts from 1 to N , it checks whether the numbers are divisible by 3, 5 or both of them. It simply prints out Uc for 3, Bes for 5 and UcBes for 3,5. If the command line argument is empty, it does the same operation but the loop goes to 1 to 20.
I am having this error "Too many arguments at line 11,15 and 19".
Here is the code:
#!/bin/bash
if [ ! -z $1 ]; then
for i in `seq 1 $1`
do
if [ [$i % 3] -eq 0 ]; then
echo "Uc"
elif [ i % 5 -eq 0 ]; then
echo "Bes"
elif [ i % 3 -eq 0 ] && [ i % 5 -eq 0 ]
then
echo "UcBes"
else
echo "$i"
fi
done
elif [ -z $1 ]
then
for i in {1..20}
do
if [ i % 3 -eq 0 ]
then
echo "Uc"
elif [ i % 5 -eq 0 ]
then
echo "Bes"
elif [ i % 3 -eq 0 ] && [ i % 5 -eq 0 ]
then
echo "UcBes"
else
echo "$i"
fi
done
else
echo "heheheh"
fi
Note that [ is actually synonym for the test builtin in shell (try which [ in your terminal), and not a conditional syntax like other languages, so you cannot do:
if [ [$i % 3] -eq 0 ]; then
Moreover, always make sure that there is at least one space between [, ], and the variables that comprise the logical condition check in between them.
The syntax for evaluating an expression such as modulo is enclosure by $((...)), and the variable names inside need not be prefixed by $:
remainder=$((i % 3))
if [ $remainder -eq 0 ]; then
You should probably use something like :
if [ $(($i % 3)) -eq 0 ]
instead of
if [ $i % 3 -eq 0 ]
if [ [$i % 3] -eq 0 ]
Your script could be greatly simplified. For example:
#!/bin/sh
n=0
while test $(( ++n )) -le ${1:-20}; do
t=$n
expr $n % 3 > /dev/null || { printf Uc; t=; }
expr $n % 5 > /dev/null || { printf Bes; t=; }
echo $t
done
gives slightly different error messages if the argument is not an integer, but otherwise behaves the same.
Related
I have a slight problem with my BASH script that I do not know the cause.
Please take a look at this simple script.
#!/bin/bash
# Given two integers X and Y. find their sum, difference, product, and quotient
# Constraints
# -100 <= X,Y <= 100
# Y != 0
# Output format
# Four lines containing the sum (X+Y), difference (X-Y), product (X x Y), and the quotient (X // Y) respectively.
# (While computing the quotient print only the integer part)
# Read data
echo "Please input for x and y!"
read x
read y
declare -i MIN=-100
declare -i MAX=100
# Checks if the valued read is in the constraints
if [ $x -gt $MAX ] || [ $x -lt $MIN ];
then
echo "Error!"
exit 1;
elif [ $y -gt $MAX ] || [$y -lt $MIN ] || [$y -eq 0];
then
echo "Error"
exit 1;
else
for operator in {"+","-","*","/",}; do echo "$x $operator $y" | bc; done
fi
The output of the script above is as follow.
Please input for x and y!
1
2
worldOfNumbers.sh: line 26: [2: command not found
worldOfNumbers.sh: line 26: [2: command not found
3
-1
2
0
As you can see, there is this [2: command not found. I believe there is something wrong with my syntax however I feel like I have typed the right one.
p.s. I use Oh My ZSH to run the program. I've also tried running in VS Code however the same thing arise.
Thank you for the help.
If you put your code through shellcheck, you'll see that it is due to the lack of spaces between your variable and your bracket. Bash is a space oriented language, and [ and ] are commands just like echo or printf.
Change
elif [ $y -gt $MAX ] || [$y -lt $MIN ] || [$y -eq 0];
to
elif [ $y -gt $MAX ] || [ $y -lt $MIN ] || [ $y -eq 0 ];
^ ^ ^
Arrows added to show where spaces were added.
You can see that [ is a command if you run this at your bash prompt:
$ which [
[: shell built-in command
Because you do not have a space between [ and 2, bash assumes that you are trying to run a command called [2 and that command does not exist, as seen by your error message.
I seemed to fix it. Here is the code.
#!/bin/bash
# Given two integers X and Y. find their sum, difference, product, and quotient
# Constraints
# -100 <= X,Y <= 100
# Y != 0
# Output format
# Four lines containing the sum (X+Y), difference (X-Y), product (X x Y), and the quotient (X // Y) respectively.
# (While computing the quotient print only the integer part)
# Read data
echo "Please input for x and y!"
read x
read y
declare -i MIN=-100
declare -i MAX=100
# Checks if the valued read is in the constraints
if [ "$x" -gt $MAX ] || [ "$x" -lt $MIN ]
then
echo "Error!"
exit 1
elif [ "$y" -gt $MAX ] || [ "$y" -lt $MIN ] || [ "$y" -eq 0 ]
then
echo "Error"
exit 1
else
for operator in {"+","-","*","/",}; do echo "$x $operator $y" | bc; done
fi
There should be a space inside the [ expression ]. Pretty interesting!
I'm implementing a merge sort algorithm in bash, but looks like it loops forever and gives error on m1 and m2 subarrays. It's a bit hard to stop loop in conditions since I have to use echo and not return. Anyone have any idea why this happens?
MergeSort (){
local a=("$#")
if [ ${#a[#]} -eq 1 ]
then
echo ${a[#]}
elif [ ${#a[#]} -eq 2 ]
then
if [ ${a[0]} -gt ${a[1]} ]
then
local t=(${a[0]} ${a[1]})
echo ${t[#]}
else
echo ${a[#]}
fi
else
local p=($(( ${#a[#]} / 2 )))
local m1=$(MergeSort "${a[#]::p}")
local m2=$(MergeSort "${a[#]:p}")
local ret=()
while true
do
if [ "${#m1[#]}" > 0 ] && [ "${#m2[#]}" > 0 ]
then
if [ ${m1[0]} <= ${m2[0]} ]
then
ret+=(${m1[0]})
m1=${m1[#]:1}
else
ret+=(${m2[0]})
m2=${m2[#]:1}
fi
elif [ ${#m1[#]} > 0 ]
then
ret+=(${ret[#]} ${m1[#]})
unset m1
elif [ ${#m2[#]} > 0 ]
then
ret+=(${ret[#]} ${m2[#]})
unset m2
else
break
fi
done
fi
echo ${ret[#]}
}
a=(6 5 6 4 2)
b=$(MergeSort "${a[#]}")
echo ${b[#]}
There are multiple issues in your shell script:
you should use -gt instead of > for numeric comparisons on array lengths
<= is not a supported string comparison operator. You should use < and quote it as '<', or better use '>' and transpose actions to preserve sort stability.
there is no need for local t, and your code does not swap the arguments. Just use echo ${a[1]} ${a[0]}
you must parse the result of recursive calls to MergeSort as arrays: local m1=($(MergeSort "${a[#]::p}"))
when popping initial elements from m1 and m2, you must reparse as arrays: m1=(${m1[#]:1})
instead of ret+=(${ret[#]} ${m1[#]}) you should just append the elements with ret+=(${m1[#]}) and instead of unset m1, you should break from the loop. As a matter of fact, if either array is empty you should just append the remaining elements from both arrays and break.
furthermore, the while true loop should be simplified as a while [ ${#m1[#]} -gt 0 ] && [ ${#m2[#]} -gt 0 ] loop followed by the tail handling.
the final echo ${ret[#]} should be moved inside the else branch of the last if
to handle embedded spaces, you should stringize all expansions but as the resulting array is expanded with echo embedded spaces that appear in the output are indistinguishable from word breaks. There is no easy workaround for this limitation.
Here is a modified version:
#!/bin/bash
MergeSort (){
local a=("$#")
if [ ${#a[#]} -eq 1 ]; then
echo "${a[#]}"
elif [ ${#a[#]} -eq 2 ]; then
if [ "${a[0]}" '>' "${a[1]}" ]; then
echo "${a[1]}" "${a[0]}"
else
echo "${a[#]}"
fi
else
local p=($(( ${#a[#]} / 2 )))
local m1=($(MergeSort "${a[#]::p}"))
local m2=($(MergeSort "${a[#]:p}"))
local ret=()
while [ ${#m1[#]} -gt 0 ] && [ ${#m2[#]} -gt 0 ]; do
if [ "${m1[0]}" '>' "${m2[0]}" ]; then
ret+=("${m2[0]}")
m2=("${m2[#]:1}")
else
ret+=("${m1[0]}")
m1=("${m1[#]:1}")
fi
done
echo "${ret[#]}" "${m1[#]}" "${m2[#]}"
fi
}
a=(6 5 6 4 2 a c b c aa 00 0 000)
b=($(MergeSort "${a[#]}"))
echo "${b[#]}"
Output: 0 00 000 2 4 5 6 6 a aa b c c
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 years ago.
I'm trying to do test automation with a bash script using if then else statements but I'm running into a few errors. For one, when I try to execute it I'm doing something wrong with the variable assignment with j and k, because it tells me that the command j and the command k aren't found when I try to execute. How do you correctly create variables?
The most confusing thing though is when I try to execute the script I get an error telling me I have an unexpected token near fi, and then it just says 'fi'. What am I doing wrong here?
#!/bin/bash
j = 0
k = 0
echo Test1:
echo -ne "0\nIn\nUG\n" | /u/cgi_web/Tuition/cost
echo Test2:
echo -ne "0\nOut\nUG\n" | /u/cgi_web/Tuition/cost
echo Test3:
echo -ne "0\nIn\nGR\n" | /u/cgi_web/Tuition/cost
echo Test4:
echo -ne "0\nOut\nGR\n" | /u/cgi_web/Tuition/cost
for i in {1..17}
do
echo Test$((i+4)):
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
$j = 1
echo -ne "$i\nIn\nUG\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 0 ] then
$k = 1
echo -ne "$i\nIn\nGR\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 1 ] then
$j = 0
echo -ne "$i\nOut\nUG\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 0 ] && [ "$k" -eq 1 ] then
$k = 0
echo -ne "$i\nOut\nGR\n" | /u/cgi_web/Tuition/cost
fi
done
EDIT: I figure out the variable issue with j and k, I had to remove the spaces in the statement.
Bash if statements require a semi-colon before the then:
if [ condition ] || [ condition ]; then
# code
elif [ condition ] && [ condition ]; then
# code
fi
For example.
To help anyone who might look at this for help in the future, I figured I'd answer my own question with all the syntax errors I found from my own testing and with the helpful responses of others.
To start the variable assignment:
j = 0
you can't have spaces in between, so it would be:
j=0
Also if statements need a space between if and the bracket and need a semicolon after the last bracket before then. Therefore my incorrect if statement
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
becomes
if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]; then
or instead of a semicolon you can have a new line between the bracket, so it would become
if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]
then
I'm nearly done writing a script for an assignment, but am having some trouble thinking of how to do this final part.
My problem is within a while loop; it prints out the number based on the IF statements, the number entered will always be an even number.
The IFs aren't connected by else/elif because the number should be able to printed out if it applies to more than 1 of the statements.
I want to print $starting on every loop if it doesn't meet any of the IF conditions, but if it does I don't want to print it. Can anyone see how to do that?
while [[ $starting -lt $ending ]]; do
if [ $((starting %7)) -eq 0 ]
then
echo "$starting red"
fi
if [ $((starting % 11)) -eq 0 ]
then
echo "$starting green"
fi
if [ $((starting % 13)) -eq 0 ]
then
echo "$starting blue"
fi
starting=$((starting + 2))
done
Keep track of whether you've done what you want to do in a variable:
while [[ $starting -lt $ending ]]; do
handled=0
if [ $((starting %7)) -eq 0 ]
then
echo "$starting red"
handled=1
fi
if [ $((starting % 11)) -eq 0 ]
then
echo "$starting green"
handled=1
fi
if [ $((starting % 13)) -eq 0 ]
then
echo "$starting blue"
handled=1
fi
if ! (( handled ))
then
echo "$starting didn't match anything"
fi
starting=$((starting + 2))
done
Add another if at the end that checks if none of the previous if statements are true. if !(starting%7==0 or starting%11==0 or starting%13==0) => echo starting
I was modifying an script didn't know how to write more than one condition in an if statement. I want to connect the two condition with an AND.
if [ envoi1 -eq 2 ];then
if [ envoi2 -eq 0 ];then
echo 'Ahora mismo.'
envoi = 1
fi
else
if [ envoi2 -eq 1 ];then
if [ envoi1 -eq 1 ];then
echo 'Situacion Normal.'
envoi = 1
fi
else
echo 'Raruno'
envoi=`expr $envoi1 + envoi2`
fi
fi
Now i use nested if to do the same but the code it's not so clear for me.
try this:
if [ $envoi1 -eq 2 ] && [ $envoi2 -eq 0 ] ; then
envoi = 1
fi
In bash, you can use [[ as follows:
if [[ $envoi2 -eq 1 && $envoi1 -eq 1 ]]; then
echo "Situacion Normal."
envoi=1
fi
However, [[ is not POSIX and will not work if you are using the /bin/sh shell. So if portability is desired use:
if [ $envoi2 -eq 1 -a $envoi1 -eq 1 ]; then
echo "Situacion Normal."
envoi=1
fi
Also note that when assigning variables you should not have any spaces on either side of the =.