How to call another script within a script with bash - 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

Related

Complicated if-then-else statement

So, I simplified this code. Every time it runs, else or $msg4 is always executed. How do I change it so it only does else if the $nick part doesn't match?
if [ "$who" = "$nick1" ]
then echo $msg1
fi
if [ "$who" = "$nick2" ]
then echo $msg2
fi
if [ "$who" = "$nick3" ]
then echo $msg3
else $msg4
fi
Here you can read how Bash if statements work: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#Conditional-Constructs
There you can see there is an elif which you should use to chain multiple if - else things together so that the final else is only executed if none of the if statements match. Result:
if [ "$who" = "$nick1" ]
then
echo $msg1
elif [ "$who" = "$nick2" ]
then
echo $msg2
elif [ "$who" = "$nick3" ]
then
echo $msg3
else
echo $msg4
fi
You can also write the then on the same line as if if you add a ; before then:
if [ "$who" = "$nick1" ]; then
echo $msg1
elif [ "$who" = "$nick2" ]; then
echo $msg2
elif [ "$who" = "$nick3" ]; then
echo $msg3
else
echo $msg4
fi
This is often easier to read.
Use case .. esac
case "$who" in
"$nick1") echo "$msg1";;
"$nick2") echo "$msg2";;
"$nick3") echo "$msg3";;
*) echo "$msg4";;
esac

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

Bash; conditional statement echoing numbers

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

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

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