Expected result not return in shell script - shell

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

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

script for testing if number is in interval

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

Shell script avoiding that 2 options can be used at the same time

I'm writing a shell script
it works great, the only problem I have is that I want to avoid the possibility of using both options -d) and -x) at the same time when executing my command with my parameters in directories.
Could this be possible with a minimal change in my code?
#!/bin/bash
dir=$1
if [ $# -lt 1 ] ; then
echo "ERROR: no argument"
exit 1 # pas 0
else
case $2
in
-d)
mv $dir/ /tmp/
echo 'moving with -d'
;;
-x)
for f in "$dir"/*; do [[ -x $f ]] && mv "$f" /tmp; done
echo 'moving executables'
;;
*)
mv $dir/* /tmp/
echo 'no flag passed so moving all'
echo "mv $dir/* /tmp/"
;;
esac
fi
I would do it the other way: first extract options, then "if" it.
#!/bin/bash
dir=$1
shift
while [ $# -gt 0 ] ; do
case $1
in
-d)
D_OPTION_SELECTED=1
;;
-x)
X_OPTION_SELECTED=1
;;
esac
shift
done
help() {
echo "Usage $0 dir [-x or -d]";
}
if [[ "$dir" == "" ]]; then help; exit 1; fi
if [[ $D_OPTION_SELECTED -gt 0 && $X_OPTION_SELECTED -gt 0 ]]; then help; exit 1; fi
if [[ $D_OPTION_SELECTED -gt 0 ]]; then echo D selected; fi
if [[ $X_OPTION_SELECTED -gt 0 ]]; then echo X selected; fi
But please remember that the good rule is to allow options at first places. So the better version would be:
#!/bin/bash
while [ $# -gt 0 ] ; do
case $1
in
-d)
D_OPTION_SELECTED=1
;;
-x)
X_OPTION_SELECTED=1
;;
*)
dir=$1
;;
esac
shift
done
help() {
echo "Usage $0 [-x or -d] dir";
}
if [[ "$dir" == "" ]]; then help; exit 1; fi
if [[ $D_OPTION_SELECTED -gt 0 && $X_OPTION_SELECTED -gt 0 ]]; then help; exit 1; fi
if [[ $D_OPTION_SELECTED -gt 0 ]]; then echo D selected; fi
if [[ $X_OPTION_SELECTED -gt 0 ]]; then echo X selected; fi
echo dir=$dir

comparing $var to

I have a test script the needs to read the variable 'LAB' and e-mail the correct company.
I've looked but can't find anything that has worked yet.
Any thoughts?
#!
#
LAB=3
#
if [ "$LAB" = "$1" ];then
echo "Got Zumbrota" && ./mailZ
fi
#
if [ "$LAB" = "$2" ];then
echo "Got Barron" && ./mailB
fi
#
if [ "$LAB" = "$3" ];then
echo "Got Stearns" && ./mailS
fi
If this a bash script, start your file with
#!/bin/bash
and use -eq for integer comparison and since LAB is an integer in your script
if [ $LAB -eq $1 ]
These cascading if statements can be condensed into a case statement:
case "$LAB" in
1) echo "Got Zumbrota" && ./mailZ
;;
2) echo "Got Barron" && ./mailB
;;
3) echo "Got Stearns" && ./mailS
;;
*) echo "don't know what to do with $LAB"
;;
esac

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