Case Statement Troubles

Hi,
I'm attempting to create case statement in a ksh script that does the following:

Run a uname command against the box and use that value for $HOSTNAME object. Then, if hostname has AIX in it, then use the lsldap command to try to bind, then print $HOSTNAME:yes or $HOSTNAME:no, depending on bind results. Then, if AIX wasn't in the $HOSTNAME, look for RHEL and do the same. It keeps errororing and I have tried so many examples and beat my head against it for so long I thought maybe someone with more experience could help.

Here's the script:

HOSTNAME=$(uname -n)

cd /tmp

echo "Line to catch HHLD Connectivity status"

case ${HOSTNAME} in

[AIX]*) 
	lsldap -a passwd rzttwp > /dev/null 2>&1
ret=$?
 
if [ $ret -ne 0 ]; then
	echo "!!!:${HOSTNAME}:no"
	    else
	echo "!!!:${HOSTNAME}:yes"
	

[RHEL]*)
	getent passwd rzttwp > /dev/null 2>&1    
ret=$?
 
if [ $ret -ne 0 ]; then
	echo "!!!:${HOSTNAME}:no"
	    else
	echo "!!!:${HOSTNAME}:yes"

I've tried it with and without the ), with and without ;; in between the :yes" and [RHEL]), tried using |, tried all kinds of permutations. It keeps erroring out. I'm trying to run this on RHEL5.3.

Any help, insights, or pointers you may be able to provide is greatly appreciated!

Thank you,

D

Is this the entire script? If so, I don't see the end of the case statement:

case 

      code

esac
1 Like

Usual requests.
Please post sample output from "uname -n" for your variety of servers.

General answer:

case "<variable_to_test>" in
         "value1") 
                  # commands
         ;;
         "value2")
                  # commands
         ;;
         "value3")
                  # commands
         ;;
         *)
                  # commands to deal with whatever is left
         ;;
esac

Based on you previous posts, have you considered attending a training course?

Ps. "It keeps erroring" is of no use to technical support persons. You need to describe your environment (O/S and Shell etc.) and show what you typed, what you expected you happen and what actually happened. Otherwise you may be depicted as someone who rides a horse and lassos wild animals.

Imho. Persons who type random commands to try to beat syntax errors should be locked out. Yep it's been a bad day!

1 Like

I added that, it still errors out on line 49, which is where the [RHEL]*) is. It looks like this now:

HOSTNAME=$(uname -n)

cd /tmp

echo "Line to catch HHLD Connectivity status"

case ${HOSTNAME} in

[AIX]*) 
	lsldap -a passwd rzttwp > /dev/null 2>&1
ret=$?
 
if [ $ret -ne 0 ]; then
	echo "!!!:${HOSTNAME}:no"
	    else
	echo "!!!:${HOSTNAME}:yes"
	

[RHEL]*)
	getent passwd rzttwp > /dev/null 2>&1    
ret=$?
 
if [ $ret -ne 0 ]; then
	echo "!!!:${HOSTNAME}:no"
	    else
	echo "!!!:${HOSTNAME}:yes"

esac

---------- Post updated at 02:45 PM ---------- Previous update was at 02:41 PM ----------

Update: I removed the * signs from in front of ) signs and still get error: syntax error at line 49: `)' unexpected

so it's still buggy lol

---------- Post updated at 02:47 PM ---------- Previous update was at 02:45 PM ----------

@methyl: Yes, I have and am definitely in need of training. I am doing this for work because they need someone to do it, I'm the lucky guy. It's not somehting I usually do and I don't have time to take training right away...I do need it tho I agree.

---------- Post updated at 02:55 PM ---------- Previous update was at 02:47 PM ----------

I am making edits using notepad++ and just using WinSCP to push the edited script back up, so not vi on server. The machine OS is RHEL 5.3. The error is syntax...here's a good example: syntax error at line 46: `;;' unexpected

With this code:

HOSTNAME=$(uname -n)

cd /tmp

echo "Line to catch HHLD Connectivity status"

case ${HOSTNAME} in
	[AIX]) 
		lsldap -a passwd rzttwp > /dev/null 2>&1
	ret=$?
 
	if [ $ret -ne 0 ]; then
		echo "!!!:${HOSTNAME}:no"
			else
		echo "!!!:${HOSTNAME}:yes"
	;;
	[RHEL])
		getent passwd rzttwp > /dev/null 2>&1    
	ret=$?
 
	if [ $ret -ne 0 ]; then
		echo "!!!:${HOSTNAME}:no"
			else
		echo "!!!:${HOSTNAME}:yes"
	;;
esac

---------- Post updated at 02:56 PM ---------- Previous update was at 02:55 PM ----------

Line 46 is above the RHEL part

The problem isn't the * symbols. The problem is that you're not following the syntax, at all.

If statements work like this:

if SOMETHING
then
...
else
...
fi

Case statements work like

case SOMETHING in
case1)
        statement
        ;;
case2)
        statement
        ;;
case3)
        statement
        ;;
esac

You're forgetting fi's and ;;'s all over the place. They're not optional.

1 Like

Another fundamental of unix Bourne Shell scripting:

if [ condition ]
then
        #do something
fi

I'm becoming more sarcastic by the minute. (Big smile on face and no vitriol intended).

While I'm being sarcastic:

When you post 26 lines, we do wonder where the error message came from.

1 Like

To repeat.

Maybe somebody on this board can post a sample one from RHEL if the O/P is away from the terminal.

1 Like

It worked..thanks to all for your input you got me over the hump! Have a great evening....