Bash scripting using case?
Hi Guys im new to linux and am having trouble writing a bash script for a network delay heres the script;
#!/bin/bash # define server M3 SERVER 192.168.244.11 # don't run in anything but /opt if [[ "$PWD" != "/opt" ]]; then echo "This script must be run in /opt or not at all:D" exit fi echo "What is your preferred Network delay" echo "1) 100" echo "2) 300" echo "3) 600" echo "4) 1000" echo "5) 2000" echo "6) No i do want to use this script" read case; $case in 1) `tc qdisc add dev eth0 root netem delay 100msc qdisc add dev eth0 root netem delay 100ms ` 2) `tc qdisc add dev eth0 root netem delay 100msc qdisc add dev eth0 root netem delay 300ms` 3) `tc qdisc add dev eth0 root netem delay 100msc qdisc add dev eth0 root netem delay 600ms` 4) `tc qdisc add dev eth0 root netem delay 100msc qdisc add dev eth0 root netem delay 1000ms` 5) `tc qdisc add dev eth0 root netem delay 100msc qdisc add dev eth0 root netem delay 2000ms` 6) exit esac echo "This script will be delayed for 2 minuts" sleep NUMBER[2m]; echo "Do you wish to get your network back y/n" read NETWORK if [ "$NETWORK" == "y" ]; then `tc qdisc add dev eth0 root netem delay 100msc qdisc add dev eth0 root netem delay 0ms` fi as you can see im trying to ask questions and if the answer is what they type then i want to run a command and echo the outcome thanks guys hope u can help |
So, have you tried running this? What happened?
Incidentally, please use code tags https://www.linuxquestions.org/quest...do=bbcode#code to show your code. |
case "$case" in
1) do_1 ;; 2) do_2 ;; *) do_other ;; esac Also I don't think you want the backquotes ``. |
Thanks guys i have tried this but i got this response from the server;
./network.sh: line 24: syntax error near unexpected token `)' ./network.sh: line 24: ` 2) `tc qdisc add dev eth0 root netem delay 100msc qdisc add dev eth0 root netem delay 300ms`' i think it may be down to the back ticks im not sure |
As linosaurusroot noted, your case syntax is wrong (missing 'case' keyword) and you don't need backquotes; see http://tldp.org/LDP/Bash-Beginners-G...ect_07_03.html
You might want to add the 'set -xv' cmd thus to see what the parser is doing Code:
|
Hi guys i have now done this ;;;
#!/bin/bash # define server M3 SERVER 192.168.244.11 # don't run in anything but /opt if [[ "$PWD" != "/opt" ]]; then echo "This script must be run in /opt or not at all:D" exit fi # network delay 100mbs echo "!!!Type the delay you wish to make for example 100!!!" echo "do you wish to slow down your network by 100" echo "do you wish to slow down your network by 300" echo "do you wish to slow down your network by 600" echo "do you wish to slow down your network by 1000" echo "do you wish to slow down your network by 2000" read NETWORK if [ "$NETWORK" == "100" ]; then `tc qdisc add dev eth0 root netem delay 100ms` if [ "$NETWORK" == "300" ]; then `tc qdisc add dev eth0 root netem delay 300ms` if [ "$NETWORK" == "600" ]; then `tc qdisc add dev eth0 root netem delay 600ms` if [ "$NETWORK" == "1000" ];then `tc qdisc add dev eth0 root netem delay 1000ms` if [ "$NETWORK" == "2000" ];then `tc qdisc add dev eth0 root netem delay 2000ms` fi # Run Network echo "Do you wish to disable network delay y/n" read DISABLE if [ "$NETWORK" == "y" ];then `tc qdisc add dev eth0 root netem delay 0ms` done fi I AM GETTING THIS REPLY ./network1.sh: line 38: syntax error: unexpected end of file |
Can anyone tell me the best way to do this im new to scripting i just want to ask a list of questions and use a key 4 each answer and if the key is this run a command for that question thats all thanks
|
1. You need to bookmark/read these
http://rute.2038bug.com/index.html.gz http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://www.tldp.org/LDP/abs/html/ 2. Please use code tags to show your code, as previously requested. 3.Also, use the set -xv cmd as recommended. 4. As above you DON'T need backquotes... 5. every 'if' needs a matching 'fi' 6. use [[ ]] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS instead of [ ] 7. for numeric comparisons, use ' -eq ' instead of ' == ' |
Thanks chris for your help thats great i did listen to previous request i just posted and edited version thanks guys!!!!
|
HI AGAIN GUYS I AM NOW DOING THIS AND THE COMMANDS DONT SEEM TO BE GETTING EXECUTED ANY IDEAS??????????? TRHANKS
this meessege is getting resonded in the script; RTNETLINK answers: File exists #!/bin/bash # define server M3 SERVER 192.168.244.11 # don't run in anything but /opt if [[ "$PWD" != "/opt" ]]; then echo "This script must be run in /opt or not at all:D" exit fi # network delay 100mbs echo "do you wish to slow down your network by 100" echo "do you wish to slow down your network by 300" echo "do you wish to slow down your network by 600" echo "do you wish to slow down your network by 1000" echo "do you wish to slow down your network by 2000" read NETWORK if [[ "$NETWORK" == "100" ]];then tc qdisc add dev eth0 root netem delay 100ms fi if [[ "$NETWORK" == "300" ]];then tc qdisc add dev eth0 root netem delay 300ms fi if [[ "$NETWORK" == "600" ]];then tc qdisc add dev eth0 root netem delay 600ms fi if [[ "$NETWORK" == "1000" ]];then tc qdisc add dev eth0 root netem delay 1000ms fi if [[ "$NETWORK" == "2000" ]];then tc qdisc add dev eth0 root netem delay 2000ms fi # Run Network echo "Do you wish to disable network delay y/n" read DISABLE if [[ "$DISABLE" == "Y" ]];then `tc qdisc add dev eth0 root netem delay 0ms` fi |
Quote:
Also, please put your code between the code tags, you have been asked a few times. |
Code:
read DISABLE Code:
# don't run in anything but /opt |
Code:
#!/bin/bash |
All times are GMT -5. The time now is 08:37 AM. |