Operating System - Linux
1820636 Members
1760 Online
109626 Solutions
New Discussion юеВ

case statement in a case statement

 
thijs lankhorst_1
Frequent Advisor

case statement in a case statement

Hi there. Can anybody tell me if i can use a case statement in a case statement?

For example in the following script i get a syntax error but i am pretty sure its caused due to the fact that it doesnt like the case into case statement: (see attachment)

Any comments are much appreciated.

4 REPLIES 4
thijs lankhorst_1
Frequent Advisor

Re: case statement in a case statement

Its ok i have found the sysntax error.
I forgot to put ;; after the inner case statement
Muthukumar_5
Honored Contributor

Re: case statement in a case statement

You have to ;; in second case statements.

#!/bin/ksh

echo "What LAN card would you like to adjust: "
read card

case "$card" in

[Ee][Tt][Hh]0)
lc=eth0
;;
[Ee][Tt][Hh]1)
lc=eth1
;;
[Ee][Tt][Hh]2)
lc=eth2
;;
[Ee][Tt][Hh]3)
lc=eth3
;;
[Ee][Tt][Hh]4)
lc=eth4
;;
[Ee][Tt][Hh]5)
lc=eth5
;;
[Ee][Tt][Hh]6)
lc=eth6
;;
*)
echo "Wrong LAN card, exiting"
exit 1
;;
esac

spe=`ethtool $lc | grep -i speed | awk '{print $2}'`
dup=`ethtool $lc | grep -i duplex | awk '{print $2}'`
neg=`ethtool $lc | grep Auto | grep -v autog| awk '{print $2}'`

echo "Current settings are:"
echo
echo "Card speed(S) is: $spe"
echo "Mode(M) is: $dup"
echo "Speed Negotiation(SN) is: $neg"

echo
echo "What would you like to change (S/M/SN): "
read choice

case "$choice" in
[Ss])
echo "What card speed(10/100/1000) would you like?: "
read cs
case "$cs" in
10)
ethtool -s $lc speed 10 duplex $dup autoneg $neg
;;
100)
ethtool -s $lc speed 100 duplex $dup autoneg $neg
;;
1000)
ethtool -s $lc speed 1000 duplex $dup autoneg $neg
;;
*)
echo "Incorrect speed, exiting"
exit 1
;;
esac
;; # ADDED
[Mm])
echo "What mode(FD/HD) would you like?: "
read lm
case "$lm" in
[Ff][Dd])
ethtool -s $lc speed $spe duplex full autoneg $neg
;;
[Hh][Dd])
ethtool -s $lc speed $spe duplex half autoneg $neg
;;
*)
echo "Incorrect mode, exiting"
exit 1
;;
esac
;; # ADDED
[Ss][Nn])
echo "What would you like AutoNegotiation(ON/OFF) to be?: "
read an
case "$an" in
[Oo][Nn])
ethtool -s $lc speed $spe duplex $dup autoneg on
;;
[Oo][Ff][Ff])
ethtool -s $lc speed $spe duplex $dup autoneg off
;;
*)
echo "Incorrect choice, exiting"
exit 1
;;
esac
;; # ADDED
*)
echo "Incorrect choice, exiting"
exit 1

esac

exit 0

## END ###

# ADDED gives new lines added.

--
Muthu
Easy to suggest when don't know about the problem!
thijs lankhorst_1
Frequent Advisor

Re: case statement in a case statement

Thanks Muthukumar, just found it myself. Would have given you all the 10 points if i had not closed the thread. Sorry.
Muthukumar_5
Honored Contributor

Re: case statement in a case statement

Your greetings are more valuable than points ;)

A tip to you: Use sh -x <script> to get debugging informations always.

Keep posting.

--
Muthu
Easy to suggest when don't know about the problem!