If Selection statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If Selection statement
# 1  
Old 11-24-2011
If Selection statement

ok im kinda stuck i have a bash script with 4 options

Code:
1. Create Script
2. Show Script
3 . Delete script
4. Exit

how would i use an if statement for this? im not sure what test command i would use.
I know it would be like this (below) to display the script

Code:
if [ test command ]
then cat script1

or for the delete script

Code:
if [ test command ]
then rm script1

would i use -f?
or would i have to set a variable for each option 1,2,3,4?

I have set the variable as in the read command as

Code:
read -p "ENTER YOUR CHOICE: " SEL1

# 2  
Old 11-24-2011
look into 'select' your shell man pages.
# 3  
Old 11-24-2011
You don't really need to use "if" statements, you can use "case":
Code:
while true
do
	clear
	echo "Menu Options: "
	echo "     1. Create Script"
	echo "     2. Show Script"
	echo "     3. Delete script"
	echo "     4. Exit"
	echo " Choose: "
	read menuOpt
	
	case "${menuOpt}" in
		1) 	echo "Opt 1"
			break;;
		2) 	echo "Opt 2"
			break;;
		3) 	echo "Opt 3"
			break;;
		4) 	echo "Opt 4"
			echo "Exitting."
			exit;;
		*)	echo "Please, choose a valid option!";;
	esac
done

About the "rm -f" question, it depends on your needs! Generally, linux boxes have an alias that point rm to "rm -i" and if you want to bypass the alias, you can use "rm -f", so nothing will be asked when removing a file, otherwise, a message will be printed.
Code:
# echo a > test.txt
# alias rm="rm -i"
# rm test.txt
rm: remove regular file `test.txt'?
# rm -f test.txt
#

I hope it helps!

Last edited by felipe.vinturin; 11-24-2011 at 12:13 PM..
This User Gave Thanks to felipe.vinturin For This Post:
# 4  
Old 11-27-2011
Code:
while true
do
    clear
    echo "Menu Options: "
    echo "     1. Create Script"
    echo "     2. Show Script"
    echo "     3. Delete script"
    echo "     4. Exit"
    echo " Choose: "
    read menuOpt
    
    case "${menuOpt}" in
        1)     echo "Enter name"
            break;;

Im having some trouble with this part, i get everything to show up but when it comes to "Choose" i press 1 and nothing happens. I also tried the read -p instead of a echo. Didnt work

And also can if statements be put into this loop to? Like after they enter a name i need them to choose a colour and if they enter or spel a color wrong they have to reenter it?
# 5  
Old 11-27-2011
Code:
#!/bin/ksh

oldIFS=$IFS
IFS='
'

actions='Create Script
Show Script
Delete script
Exit'
PS3='Pick one of the above: '

select i in ${actions}
do
   case $REPLY in
      1)  echo 'Enter name' ;;
      2) cat script ;;
      3) rm script ;;
      4) exit 0 ;;
      "") print -u2 you must select one of the above [${actions}] ;;
   esac
   REPLY=''
done

# 6  
Old 11-27-2011
ok i got everything to work

is there anyway which i can check the users input against a text file? in the loop?

im using a grep statement

grep -w "$cars" $carsfile

but it says no such file or directory tried the -f option to

Last edited by gangsta; 11-27-2011 at 02:24 PM..
# 7  
Old 11-27-2011
Quote:
Originally Posted by gangsta
ok i got everything to work

is there anyway which i can check the users input against a text file? in the loop?
Please, be more specific with examples.
You can always use *NIX utils to validate input: file-based or otherwise.
Depending on what your specifics are, you may use grep, sed, awk, perl,etc...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selection from array

Hi, I need some help taking a selection from a command and adding part of the output to an array. I'd like to read the items into the array, have the user chose an option from the array and put the item from column 1 into a variable. The command is: awless -l list routetables --columns... (7 Replies)
Discussion started by: bignellrp
7 Replies

2. Shell Programming and Scripting

Multi pattern selection

I'm confused with what to use (awk or grep) in this case as i need to select 2 corresponding patterns. "SName" & "ESys" in a appln config file which looks like this; SName=abc123 ESys=xyz456 Host=xxx Port=yyy I used awk and didn't get any output for multi-pattern search. $ awk -F"="... (7 Replies)
Discussion started by: sam_bd
7 Replies

3. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

4. Shell Programming and Scripting

Date selection

Hi All, i have log file sample data is 2010/10/09|04:00:00.392|15Minute|BW=0|192.168.0.1 2010/10/08|04:00:00.392|15Minute|BW=0|192.168.0.1 2010/10/07|04:00:00.392|15Minute|BW=0|192.168.0.1 2010/10/05|04:00:00.392|15Minute|BW=0|192.168.0.1 2010/10/03|04:00:00.392|15Minute|BW=0|192.168.0.1... (2 Replies)
Discussion started by: posner
2 Replies

5. Shell Programming and Scripting

Data selection

Hi, Please note that as a programmer I cannot tell the file format or details of my production files so I have re-framed my file and taken a case of departments. Kindly guide as its related to a production requirement of data containing recors of production customer NOT A HOMEWORK :) I have... (10 Replies)
Discussion started by: test_user
10 Replies

6. Shell Programming and Scripting

Data selection

Hi, I have a file containing details of different departments . Infomration of departments is in various tags file is as below I want to create a new file from the above file which should contain only two fields belonging to one department format There are multiple files... (1 Reply)
Discussion started by: test_user
1 Replies

7. Shell Programming and Scripting

simple selection between two words

Hi guys, got an issue with a relatively simple bit of awk but stumping me. Want this to run on several different lines. The first is: INPUT="<Cell ss:StyleID="s70"><Data ss:Type="String">ND</Data></Cell>" echo $INPUT | awk 'NR==1022' | awk '/String">/,/</'Gives me no output atm. The output... (3 Replies)
Discussion started by: mikepegg
3 Replies

8. Post Here to Contact Site Administrators and Moderators

Opt out selection

Maybe I'm missing something but when I go to CP->Options, I see the box for selecting which forum to opt out of but no way to set it. (5 Replies)
Discussion started by: drhowarddrfine
5 Replies

9. UNIX for Advanced & Expert Users

tray selection

Hello All, Could anyone help me how to selecting the trays in unix . Thanks, Amit kul (3 Replies)
Discussion started by: amit kul
3 Replies

10. Shell Programming and Scripting

Array and Selection

I have a question: Y X Tabel a is a array multidimensional --> a(1024,20) I load in to array a Text from 6000 row where: in a(1,1) is present the row 1 of original text, in a(1024,1) is present then row 1024 of original test and in... (4 Replies)
Discussion started by: ZINGARO
4 Replies
Login or Register to Ask a Question