Help with control flow in a Bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with control flow in a Bash script
# 1  
Old 02-19-2013
Help with control flow in a Bash script

In my bash script I want to say "if argument 2 is anything except x, y or z, than echo this" (x y and z being words).

So my script looks like this:

Code:
if [[ $2 != {x,y,z} ]]
then
echo "unrecognized input: $2"
fi

This usually works but than I also want to say "if argument 2 IS x, y, or z, but argument 4 is not one of them, echo 'unrecognized input: $4'". So how to I format this so my script does both. I had this, but whenever argument 2 was x, y or z, and argument 4 wasn't, I still got the "unrecognized input: $2" error message.

Code:
if [[ $2 != {x,y,z} ]]
then
echo "unrecognized input: $2"
elif [[ $2 = {x,y,z} && $4 != {x,y,z} ]]
then
echo "unrecognized input: $4"
fi


Last edited by Jrodicon; 02-19-2013 at 04:24 PM..
# 2  
Old 02-19-2013
You don't need to re-test $2 in line 4, as you have already determined that it is one of x, y or z in line 1.
If $2 or $4 is null, the script will die.
# 3  
Old 02-19-2013
Yes, use " around variables so if missing there is an empty string to keep it legal.
# 4  
Old 02-19-2013
Thanks for the advice but I still couldn't get it to quite do what I want it to do. I have added in a loop after some advice from a computer scientist. The updated version of the problematic section of my code:

Code:
list="inches hands feet yards furlongs miles leagues au"
for item in $list
do      
        if [[ "$2" = "$item" ]]
        then
                if [[ "$4" != "$item" ]]
                then
                        echo "unrecognized unit: $4"
                fi
        else
                echo "unrecognized unit: $2"
        fi
done

This of course produces 8 error messages for any input where either $2 or $4 is not part of $list. So to put it in english, this is what I want to do: "go through the whole list and if ANY match with $2, check $4, if none match, produce 1 error message (unrecognized unit: $4). Also if nothing in the list matches with $2, produce 1 error message (unrecognized unit: $4)". What do I need to do/change to get my code to do this.
# 5  
Old 02-19-2013
'break' out of the loop after printing the first error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Access a complete flow with CURL + bash shell

Hello Experts , I have an use case which needed your help . I have been using google for 2 days buy couldn`t succed , i believe i can get the help here. Here is my use case to run on bash shell 1. Access an URL -- in script , it will be mentioned as inputURL 2. Once i accessed the URL... (5 Replies)
Discussion started by: radha254
5 Replies

2. Shell Programming and Scripting

Access a complete flow with CURL + bash shell

Hello Experts , I have an use case which needed your help . I have been using google for 2 days buy couldn`t succed , i believe i can get the help here. Here is my use case to run on bash shell 1. Access an URL -- in script , it will be mentioned as inputURL 2. Once i accessed the URL... (1 Reply)
Discussion started by: radha254
1 Replies

3. UNIX for Dummies Questions & Answers

Flow control state changed in server logs

Hi, We observe below logs from switch - the database servers rebooted becaause they couldn't do I/O on vfiler -Any pointers looking at below logs please? Switch logs: 2016 Apr 30 07:41:16.729 EAG-ECOM-POD111GPU-SWF1 %ETHPORT-5-IF_DOWN_LINK_FAILURE: Interface Ethernet152/1/8 is down (Link... (0 Replies)
Discussion started by: admin_db
0 Replies

4. Shell Programming and Scripting

EXPECT Bash Script Error Control

Hello Geeks once more, Thanks for all the help you have been rendering.. I have a script that depends on the output of an expect statement but sometimes the main script misbehaves which I believe is a result of SSH communication error, how can I apply an error control to know whether the... (2 Replies)
Discussion started by: infinitydon
2 Replies

5. Shell Programming and Scripting

Backup script using Korn Flow Control

I am wiping off the dust to my shell scipting days and had this question: I have this script that I have created, and cannot figure out where my flow control issue is within this script. #!/bin/ksh #Basic script to backup server #Home directories to the external ... (2 Replies)
Discussion started by: metallica1973
2 Replies

6. Shell Programming and Scripting

Flow Control in CSH

hi , I am new to scripting, i have a doubt can any one pls solve it for me the code is not working set users = (user1 user2 user3) echo The users are echo $users echo Enter the USER NAME set USER_NAME = $< set i = 1; for ( i = 1; i <= $#users; i++ ) if ( $USER_NAME == $users )... (1 Reply)
Discussion started by: Manju87
1 Replies

7. Shell Programming and Scripting

Python script - control flow statements

Hi guys.I'm just beginner of python. I'm just trying to do some analysis on simple input file. it has 6 columns and i want to consider k,l and m,n if i and j are + after that checking which value is greater or lower in k,l and m,n I have included logic header just to explain what I was... (4 Replies)
Discussion started by: repinementer
4 Replies

8. IP Networking

Disabling 802.3x flow control

I have a server I would like to disable 802.3x flow control on. The host is Linux (CentOS 4.4 x86_64 w/ 2.6.9-42.0.3.EL kernel,) and I'm using the ns83820 driver for the ethernet interface in question. I've tried looking at the driver parameters (modinfo ns83820) and using ethtool (ethtool -a... (0 Replies)
Discussion started by: LivinFree
0 Replies

9. Programming

dilemma in control flow

hello im facing a queer problem when i execute the foll code in unix # include <stdio.h> # include <unistd.h> main(int argc,char *argv) { FILE *fp = fopen("/ras/chirag/fifotest/file.fifo","a"); int i=1; fprintf(fp,argv); printf("I SLEEP"); system("date"); for (i=0;i<50;i++)... (2 Replies)
Discussion started by: tej.buch
2 Replies

10. UNIX for Dummies Questions & Answers

how to assign correct control flow?

how can i create a script with a correct control flow/loop(?) to provide output from 3 files? file1 one two three file2 yellow red orange file3 banana apples cantaloupes output file: one (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question