LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash scripting using case? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-using-case-4175435231/)

millscourtney47 11-02-2012 05:01 AM

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

chrism01 11-02-2012 05:12 AM

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.

linosaurusroot 11-02-2012 05:16 AM

case "$case" in
1) do_1 ;;
2) do_2 ;;
*) do_other ;;
esac

Also I don't think you want the backquotes ``.

millscourtney47 11-02-2012 05:25 AM

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

chrism01 11-02-2012 05:29 AM

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:


#!/bin/bash
set -xv


millscourtney47 11-02-2012 06:28 AM

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

millscourtney47 11-02-2012 06:45 AM

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

chrism01 11-02-2012 06:52 AM

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

millscourtney47 11-02-2012 07:07 AM

Thanks chris for your help thats great i did listen to previous request i just posted and edited version thanks guys!!!!

millscourtney47 11-02-2012 07:28 AM

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

jkirchner 11-02-2012 07:35 AM

Quote:

Originally Posted by millscourtney47 (Post 4820411)
Thanks chris for your help thats great i did listen to previous request i just posted and edited version thanks guys!!!!

Actually, you are not doing anything that has been recommended. You really need to go back and reread prior posts, especially chrism01's post above.

Also, please put your code between the code tags, you have been asked a few times.

linosaurusroot 11-02-2012 07:48 AM

Code:

read DISABLE
if [ "$NETWORK" == "y" ];then

Testing the wrong variable will not get you far.

Code:

# don't run in anything but /opt
if [[ "$PWD" != "/opt" ]]; then
echo "This script must be run in /opt or not at all"
exit
fi

cd /opt || exit  # Do the right thing with no complaint at the user.

Computers are logical things - apply some thought and not hasty trials of the first ideas that you have.

unSpawn 11-02-2012 08:45 AM

Code:

#!/bin/bash
set -vxe; LANG=C; LC_ALL=C; export LANG LC_ALL; cd /opt || exit 1
TC_CMD="tc qdisc add dev eth0 root netem delay"; doSelect() { select VAR in revert \
10 100 1000 "enter custom value"; do case $VAR in revert) DELAY=0;; *custom*) echo \
"Enter delay number:"; read VALUE; [ -z ${VALUE//[0-9]/} ] && DELAY="${VALUE}" || \
exit 1;; *) DELAY=$VAR;; esac; break; done; }; parseOpts() { while getopts d:rh OPT;
do case "${OPT}" in d) [ -z ${OPTARG//[0-9]/} ] && DELAY="${OPTARG}" || exit 1;; r) \
DELAY=0;; h) echo "You need help."; exit 1;; esac; done; }; [ $# -eq 0 ] && doSelect \
|| parseOpts $@; [ -z $DELAY ] || echo "$TC_CMD $DELAY"; exit 0



All times are GMT -5. The time now is 08:37 AM.