LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2012, 06:01 AM   #1
millscourtney47
LQ Newbie
 
Registered: Nov 2012
Posts: 7

Rep: Reputation: Disabled
Cool 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"
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

Last edited by millscourtney47; 11-02-2012 at 06:16 AM.
 
Old 11-02-2012, 06:12 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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.
 
Old 11-02-2012, 06:16 AM   #3
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
case "$case" in
1) do_1 ;;
2) do_2 ;;
*) do_other ;;
esac

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

Last edited by linosaurusroot; 11-02-2012 at 06:17 AM.
 
Old 11-02-2012, 06:25 AM   #4
millscourtney47
LQ Newbie
 
Registered: Nov 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-02-2012, 06:29 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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
 
1 members found this post helpful.
Old 11-02-2012, 07:28 AM   #6
millscourtney47
LQ Newbie
 
Registered: Nov 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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"
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

Last edited by millscourtney47; 11-02-2012 at 07:29 AM.
 
Old 11-02-2012, 07:45 AM   #7
millscourtney47
LQ Newbie
 
Registered: Nov 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-02-2012, 07:52 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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 ' == '
 
1 members found this post helpful.
Old 11-02-2012, 08:07 AM   #9
millscourtney47
LQ Newbie
 
Registered: Nov 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks chris for your help thats great i did listen to previous request i just posted and edited version thanks guys!!!!
 
Old 11-02-2012, 08:28 AM   #10
millscourtney47
LQ Newbie
 
Registered: Nov 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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"
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

Last edited by millscourtney47; 11-02-2012 at 08:30 AM.
 
Old 11-02-2012, 08:35 AM   #11
jkirchner
Member
 
Registered: Apr 2007
Location: West Virginia
Distribution: LinuxMint 18
Posts: 747

Rep: Reputation: 182Reputation: 182
Quote:
Originally Posted by millscourtney47 View Post
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.
 
Old 11-02-2012, 08:48 AM   #12
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 11-02-2012, 09:45 AM   #13
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change Case of a string(BASH Scripting)? pinga123 Linux - Newbie 4 07-09-2010 01:31 AM
ignore case in if statement shell scripting saiteju Programming 2 02-27-2010 02:49 AM
case problems when scripting in bash camilla_darling Programming 2 03-19-2009 01:37 PM
KSH Scripting case statement..... ']['HeBroken Programming 1 12-10-2004 11:38 AM
Shell scripting yes/no case? Squeak2704 Programming 1 04-09-2004 03:05 PM


All times are GMT -5. The time now is 06:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration