need help in bash script for if and greater than [duplicate] - bash

This question already has an answer here:
Why do some people put a semicolon after an if condition in shell scripts? [duplicate]
(1 answer)
Closed 3 years ago.
i have
syntax error near unexpected token `elif'
`elif [[$# -gt 31]]
when i execute following script (its part from script by "Fred Weinhaus" for text-cleaner) by python 3
if [ $# -eq 0 ] then # help information
echo ""
usage2
exit 0 elif [ $# -gt 31 ] then errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---" else while [ $# -gt 0 ] do # get parameter values case "$1" in
-h|-help) # help information
echo ""
usage2
exit 0
;;
how i solve this problem?

Your script has many mistakes. My fix may not be correct.
Define "usage2".
#!/bin/bash
if [ $# -eq 0 ]; then # help information
echo ""
#usage2
exit 0
elif [ $# -gt 31 ]; then
errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else while [ $# -gt 0 ]; do # get parameter values
case "$1" in
-h|-help) # help information
echo ""
#usage2
exit 0
;;
esac
done
fi
There are no errors in lines no 201 and 206.
Try this code.
#!/bin/bash
usage2(){
# dummy
:
}
if [[ $# -eq 0 ]]
then
echo ""
usage2
exit 0
elif [[ $# -gt 31 ]]
then
errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else
while [[ $# -gt 0 ]]
do
:
done
fi

Related

bash script question [: too many arguments

This is the bash script I tested :
#!/bin/bash
if [ 8 -gt 6 ] && [ 10 -eq 10 ];
then
echo "Conditions are true"
fi
but get a error [: too many arguments.
I tried adding two square brackets, the error is syntax error near unexpected token `[['
#!/bin/bash
if [[ 8 -gt 6 ]] && [[ 10 -eq 10 ]];
then
echo "Conditions are true"
fi
I modified the code to be like this :
if [ 8 -gt 6 ];
then
echo "Conditions are true"
fi
This is okay .
How to solve this problem? thanks.

Bash function always ignores the else block

When I run the below code with exact two arguments, the else block doesn't get executed.
If I take out the if else block out of the function, everything works fine.
#!/bin/bash
usage() {
if [[ $# -gt 2 || $# -lt 2 ]]; then
echo "insufficient args"
else
if [[ $# -eq 2 ]]; then
echo "continuing with the script"
fi
fi
}
usage
In this situation, the function usage is receiving 0 arguments from the call.
Change the call to usage $#, which will pass the command line arguments to the usage function.
#!/bin/bash
usage() {
if [[ $# -gt 2 || $# -lt 2 ]]; then
echo "insufficient args"
else
if [[ $# -eq 2 ]]; then
echo "continuing with the script"
fi
fi
}
usage "$#"

script8.sh: line 9: [: missing `]' [duplicate]

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

If statement with two or more conditions

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 =.

How to count and check passed arguments?

How can I translate the following Ruby code to Bash?
if ARGV.length == 0
abort "\nError: The project name is required. Aborting...\n\n"
elsif ARGV.length > 2
abort "\nError: The program takes two arguments maximum. Aborting...\n\n"
end
#!/bin/bash
USAGE="$0: <project name> [subproject attribute]"
if [ $# -lt 1 ]; then echo -e "Error: The project name is required.\n$USAGE" >&2; exit 1; fi
if [ $# -gt 2 ]; then echo -e "Error: Two arguments maximum.\n$USAGE" >&2; exit 1; fi
The following should be what you need:
#!/bin/bash
if [ $# -eq 0 ]; then
echo -e "\nError: The project name is required. Aborting...\n\n"
exit 1
elif [ $# -gt 2 ]; then
echo -e "\nError: The program takes two arguments maximum. Aborting...\n\n"
exit 1
fi
The TLDP bash guide is very good if you are looking to learn bash, see TDLP Bash guide.
Maybe:
#!/bin/bash
function functionName {
if [ $# = 0 ]
then echo "\nError: The project name is required. Aborting...\n\n"; exit 1
fi
if [ $# \> 2 ]
then echo "\nError: The program takes two arguments maximum. Aborting...\n\n"; exit 1
fi
}
functionName a

Resources