While true Breaking Bash - bash

I've been writing some bash scripts and for some reason, what used to be working code is no longer working. I had not done any changes, but the while true; do now breaks my bash script.
Here's part of it. Catting README.txt works flawlessly. The terminal echoes "Test..." correctly, but as soon as it hits "while true" it closes, regardless of the $exec bash that keeps it open after finishing.
#!/bin/bash
cat README.txt
echo
echo
sleep 2
echo
echo
echo "Test..."
sleep 3
#The option to read the README.txt file above
while true; do
read -p "Now is your chance to go back and read.
Or press y/n to continue." yn
case $yn in
[Yy]* ) break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
$exec bash
Any help would be greatly appreciated as I'm beginning to think it might be more than a code error.
UPDATE
After running the script by using "bash -x 111.sh", the error message I receive is
+ echo Test...
Test...
+ sleep 3
111.sh: line 59: syntax error near unexpected token `)'
111.sh: line 59: ` [Yy]* ) break;;'
How is there an unexpected token or a syntax error when many other sources say this is in fact correct, as well as it working prior to today.

Not sure why that all of a sudden doesn't work, but I replaced all instances of the while true loop with a read -p.
read -p 'Do you have (Program) installed? [y/n]' answer
case "${answer}" in
[yY]|[yY][eE][sS])
echo;;
[nN]|[nN][oO])
apt-get install (program); echo;;
esac
This seems to work much smoother and hasn't failed me as of yet.

Related

BASH Trap CTRL+C Then Exit Script Completely

I've add a trap to my bash script so when CTRL+C is pressed a message appears Do you want to quit ? (y/n)
This works at most parts of the script, but fails at others.
I've created a simple example that shows it always failing.
#!/bin/bash
quit() {
echo "Do you want to quit ? (y/n)"
read ctrlc
if [ "$ctrlc" = 'y' ]; then
exit
fi
}
trap quit SIGINT
trap quit SIGTERM
while true; do
echo -e "\n\e[91mIs everything done ? (y/n)\e[0m"
read -i "y" -e yn
case $yn in
[Nn]* ) continue;;
[Yy]* )
echo -e "Done"
break;;
* ) echo -e "\e[91mPlease answer yes or no.\e[0m";;
esac
done
Why when I press CTRL+C does this pop up Do you want to quit ? (y/n) but not allow me to exit ? How do I solve it ?
Thanks
The above code is running without any errors in bash shell. I suspect that you have run the script in dash SHELL (some machine's default SHELL is dash).
Run your script using the below methods,
/bin/bash
or
Give executing permission to your script file (chmod 777 script.sh) and run the file like below,
./script.sh
As I commented above - inside a function, exit is treated as a synonym for return and does not terminate a program. If that is your problem, try
kill -term $$ # send this program a terminate signal
instead of just exit. It's heavy-handed but generally effective.
Note that if you also have a SIGTERM trap that will be executed.

trying to create my bash script

I am trying to create a simple bash script.
(just started working on bash scripting)
the script is simple. There is an issue with rsyslog service and from time to time dies.
I am trying to make a script to check if service is dead to restart it
till now I want to check if my conditions are ok but it seems I am getting an error. Can you advice me ?
Here is the script:
#!/bin/bash
a="dead"
b="running"
while i in $(/etc/init.d/rsyslog status | grep -o 'running\|dead');
do
if
[ "$a" == "$i" ];
then
echo "service rsyslog is dead "
if
[ "$b" == "$i" ];
then
echo "service rsyslog is running"
else
echo "nothing to do";
fi;
done
-------------
I am getting the following syntax error.
./rsyslogcheck.sh: line 17: syntax error near unexpected token done'
./rsyslogcheck.sh: line 17:done'
Thank you in advance!!
There are several problems here:
Invalid while loop syntax
Unnecessary loop: it seems you don't need a loop at all
Missing closing fi of an if that was opened
I suppose you're looking for something like this:
#!/bin/bash
status=$(/etc/init.d/rsyslog status | grep -o 'running\|dead')
case "$status" in
dead) echo "service rsyslog is dead";;
running) echo "service rsyslog is running";;
*) echo "nothing to do";;
esac

Ksh syntax error '=~'

read -p "The Process running for "$days" days continuously OK to kill this process (y/N)? " -u 4 ok
[[ "${ok}" =~ y ]] || continue
echo "Killing $pid"
kill -HUP "$pid"
fi
This is the snippet of my script ,when i am executing this it shows like
`=~' is not expected.
How to resolve it?
I'm guessing your shebang line has #!/bin/sh and so you don't have access to the full ksh syntax. If you do, ksh93 does appear to support [[ string =~ regex ]] syntax, so there's something here which doesn't add up right.
Either way, there is a construct which works just as well in classic Bourne shell which you can use instead, with the added bonus that your script will be compatible to systems where ksh is not available.
You use read -p <prompt> but that is a Bashism; the -p option to read has a quite different meaning in ksh93.
printf 'Process ran for %i days continuously, OK to kill this? ' "$days"
read -u 4 ok
case $ok in [Yy]* ) ;; *) continue ;; esac
echo "Killing $pid"
kill -HUP "$pid"
Your code looked for y anywhere in the input but I restricted that to only examine the first character.
(Your code had erratic indentation and an unpaired fi which I omitted.)
Your ? is 'how to resolve' - tripleee's suggestion looks like a solution - simplify the code - try:
if [[ "${ok} == "y" ]]
I tried copying your code snippet and I get a different error. Time for D&C - simple ksh93 script testing '=~'

How do you get user input when running a bash script from a url?

Consider this bash script :
#!/bin/bash
while true; do
read -p "Give me an answer ? y/n : " yn
case $yn in
[Yy]* ) answer=true ; break;;
[Nn]* ) answer=false ; break;;
* ) echo "Please answer yes or no.";;
esac
done
if $answer
then
echo "Doing something as you answered yes"
else
echo "Not doing anything as you answered no"
fi
When run from the command line using :
$ ./script-name.sh
It works just as expected with the script waiting for you to answer y or n.
However when I upload to a url and attempt to run it using :
$ curl http://path.to/script-name.sh | bash
I get stuck in a permanent loop with the script saying Please answer yes or no. Apparently the script is receiving some sort of input other than y or n.
Why is this? And more importantly how can I achieve user input from a bash script called from a url?
Perhaps use an explicit local redirect:
read answer < /dev/tty
You can run it like this:
bash -c "$(curl -s http://path.to/script-name.sh)"
Since you're supplying content of bash script to bash interpreter. Use curl -s for silent execution.

How to read input from the user in a bash subshell [duplicate]

Consider this bash script :
#!/bin/bash
while true; do
read -p "Give me an answer ? y/n : " yn
case $yn in
[Yy]* ) answer=true ; break;;
[Nn]* ) answer=false ; break;;
* ) echo "Please answer yes or no.";;
esac
done
if $answer
then
echo "Doing something as you answered yes"
else
echo "Not doing anything as you answered no"
fi
When run from the command line using :
$ ./script-name.sh
It works just as expected with the script waiting for you to answer y or n.
However when I upload to a url and attempt to run it using :
$ curl http://path.to/script-name.sh | bash
I get stuck in a permanent loop with the script saying Please answer yes or no. Apparently the script is receiving some sort of input other than y or n.
Why is this? And more importantly how can I achieve user input from a bash script called from a url?
Perhaps use an explicit local redirect:
read answer < /dev/tty
You can run it like this:
bash -c "$(curl -s http://path.to/script-name.sh)"
Since you're supplying content of bash script to bash interpreter. Use curl -s for silent execution.

Resources