script for testing if number is in interval - shell

I am trying to test if a number is in the interval [1;100] here is what I did:
var=10
if [ $["$var" -gt "1" ] -a $["$var" -lt "100"] ] ; then
echo "yes"
else
echo "no"
fi
however when I run the script I get the error message:
./yourscript:line 2 10 -gt 1:error syntax in expression ,any ideas why?

delete unnecessaries and use &&:
var=10
if [ $var -gt 1 ] && [ $var -lt 100 ] ; then #or with -a if [ $var -gt 1 -a $var -lt 100 ] ;
echo "yes"
else
echo "no"
fi

Related

How to call another script within a script with bash

Hello here is my primary script. The test2.sh is just an echo "it worked"
what happens when I try and call from the original loop, it gets to the correct file then echo's infinite "it worked" where it should just be doing it once.
Any idea why? I really want to have another loop called outside of the main script that won't interfere, but still learning bash =P
#!/bin/bash
number=1
while true
do
if [ "$number" -eq "1" ]; then
echo "hello 1!"
elif [ "$number" -eq "2" ]; then
echo "hello 2!"
elif [ "$number" -eq "3" ]; then
echo "hello 3!"
elif [ "$number" -eq "4" ]; then
./test2.sh & continue
fi
sleep 5
((number++))
echo $number
done
first observation & is not a logical operator, & runs the precedding command in the background. Use && for logical operations.
what you need is a break keyword not a continue keyword . If you use the break keyword, the loop will stop executing. The continue keyword only rexecutes the loop , and since number is 4 , this branch of code will always run elif [ "$number" -eq "4" ]; then
working code
#!/bin/bash
number=1
while true
do
if [ "$number" -eq "1" ]; then
echo "hello 1!"
elif [ "$number" -eq "2" ]; then
echo "hello 2!"
elif [ "$number" -eq "3" ]; then
echo "hello 3!"
elif [ "$number" -eq "4" ]; then
./test2.sh && break
fi
sleep 5
((number++))
echo $number
done
or you can do this
for number in {1..4};do
(( number == 4 )) && ./test2.sh || echo "$number"
sleep 5
done

Expected result not return in shell script

I wrote the following code in shell script:
#!/bin/bash
tput clear
a=$(date +"%k")
if [ $a -lt 12 ]
then
echo "Hi!Good Morning"
fi
if [ $a -ge 12 -a $a -le 17 ]
then
echo "Hi!Good Afternoon"
fi
if [ $a -gt 17 -a $a -le 19 ]
then
echo "Hi!Good Evening"
fi
if [ $a -gt 19 -a $a -le 24 ]
then
echo "Hi!Good Night"
fi
while [ : ]
do
echo "BCSE!!\c"
read comm
set comm
case "$1" in
[""])
continue
;;
esac
case "$1" in
["editme"])
xdg-open "$2"&
;;
esac
case "$1" in
["newd"])
mkdir -p "$2"
;;
esac
case "$1" in
["mycontent"])
if [ -f "$2" ]
then
xdg-open "$2"&
else
echo "File doesn't exist"
fi
;;
esac
case "$1" in
["exitbcse"])
break
;;
esac
case "$1" in
[*])
echo "Wrong command!!";;
esac
done
The output should be :
Hi!Good morning
BCSE!!editme filename
now the file doesn't open instead I get
Hi!Good morning
BCSE!!editme filename
BCSE!!
Instead of:
while [ : ]
You may want to write:
while :
or
while true
while [ : ] may work, but not for the right cause, that sentence runs the command [, the command [ checks the expression you wrote inside, as it is a non-empty string it returns a true value (a zero), to ilustrate this, if you run while [ false ] you will also get an infinite loop.
And in the case control structures the options must be written without [] and "".
case "$1" in
exitbcse)
break
;;
esac
Edit:
Check this example with the corrections I described above and also other fixes:
#!/bin/bash
tput clear
a=$(date +"%k")
if [ $a -lt 12 ]
then
echo "Hi!Good Morning"
elif [ $a -ge 12 -a $a -le 17 ]
then
echo "Hi!Good Afternoon"
elif [ $a -gt 17 -a $a -le 19 ]
then
echo "Hi!Good Evening"
elif [ $a -gt 19 -a $a -le 24 ]
then
echo "Hi!Good Night"
fi
while true
do
echo "BCSE!!\c"
read comm option
case "$comm" in
"")
continue
;;
"editme")
xdg-open "$option"&
;;
"newd")
mkdir -p "$option"
;;
"mycontent")
if [ -f "$option" ]
then
xdg-open "$option"&
else
echo "File doesn't exist"
fi
;;
"exitbcse")
break
;;
*)
echo "Wrong command!!";;
esac
done

Bash if statement with multiple conditions throws an error

I'm trying to write a script that will check two error flags, and in case one flag (or both) are changed it'll echo-- error happened. My script:
my_error_flag=0
my_error_flag_o=0
do something.....
if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ] || [ "$my_error_flag"="1" && "$my_error_flag_o"="2" ]]; then
echo "$my_error_flag"
else
echo "no flag"
fi
Basically, it should be, something along:
if ((a=1 or b=2) or (a=1 and b=2))
then
display error
else
no error
fi
The error I get is:
line 26: conditional binary operator expected
line 26: syntax error near `]'
line 26: `if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ] || [ "$my_error_flag"="1" && "$my_error_flag_o"="2" ]]; then'
Are my brackets messed up?
Use -a (for and) and -o (for or) operations.
tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
Update
Actually you could still use && and || with the -eq operation. So your script would be like this:
my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ] || ([ $my_error_flag -eq 1 ] && [ $my_error_flag_o -eq 2 ]); then
echo "$my_error_flag"
else
echo "no flag"
fi
Although in your case you can discard the last two expressions and just stick with one or operation like this:
my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ]; then
echo "$my_error_flag"
else
echo "no flag"
fi
You can use either [[ or (( keyword. When you use [[ keyword, you have to use string operators such as -eq, -lt. I think, (( is most preferred for arithmetic, because you can directly use operators such as ==, < and >.
Using [[ operator
a=$1
b=$2
if [[ a -eq 1 || b -eq 2 ]] || [[ a -eq 3 && b -eq 4 ]]
then
echo "Error"
else
echo "No Error"
fi
Using (( operator
a=$1
b=$2
if (( a == 1 || b == 2 )) || (( a == 3 && b == 4 ))
then
echo "Error"
else
echo "No Error"
fi
Do not use -a or -o operators Since it is not Portable.
Please try following
if ([ $dateR -ge 234 ] && [ $dateR -lt 238 ]) || ([ $dateR -ge 834 ] && [ $dateR -lt 838 ]) || ([ $dateR -ge 1434 ] && [ $dateR -lt 1438 ]) || ([ $dateR -ge 2034 ] && [ $dateR -lt 2038 ]) ;
then
echo "WORKING"
else
echo "Out of range!"
You can get some inspiration by reading an entrypoint.sh script written by the contributors from MySQL that checks whether the specified variables were set.
As the script shows, you can pipe them with -a, e.g.:
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
...
fi

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

shell script: integer expression expected

#!/bin/bash
if [$# -ne 1];
then
echo "/root/script.sh a|b"
else if [$1 ='a'];
then
echo "b"
else if [$1 ='b']; then
echo "a"
else
echo "/root/script.sh a|b"
fi
I'm getting below error while run above script in Linux.
bar.sh: line 2: [: S#: integer expression expected
a
Could you please help to remove this error?
if [$# -ne 1];
[ and ] requires spacing. Example:
if [ $# -ne 1 ];
And else if should be elif
#!/bin/bash
if [ "$#" -ne 1 ];
then
echo "/root/script.sh a|b"
elif [ "$1" ='a' ];
then
echo "b"
elif [ "$1" ='b' ]; then
echo "a"
else
echo "/root/script.sh a|b"
fi
Do not forget to quote variables. It is not every time necessary, but recommended.
Question: Why do i have -1?
Bash doesn't allow else if. Instead, use elif.
Also, you need spacing within your [...] expression.
#!/bin/bash
if [ $# -ne 1 ];
then
echo "/root/script.sh a|b"
elif [ $1 ='a' ];
then
echo "b"
elif [ $1 ='b' ]; then
echo "a"
else
echo "/root/script.sh a|b"
fi

Resources