run cmd based on input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting run cmd based on input
# 1  
Old 10-05-2011
run cmd based on input

Hi, This is what I have so far but it seems like a lot more than is necessary because....for example...user presses 2 or 3 ..... the script does the *same* thing it just depends on the directory it has to access....how can I improve this so that the code from 2 and 3 is only put once...?

Code:
#!/bin/bash

sort -u file.txt | tr '[:upper:]' '[:lower:]' > /file2.txt

# ask user what they want to do
echo -e -n "\nWhich folder would you like to process?\n
1=folder1
2=folder3
3=folder6 
4=Exit
:  "
read SERVER

if [ "$SERVER" -eq "1" ]; then

DIR=folder1

clear
echo -e -n "\n"
echo "------Entering $DIR Folder------"
ls
echo -e -n "\nDone"

elif [ "$SERVER" -eq "2" ]; then

DIR=folder3

clear
echo -e -n "\n"
echo "------Entering $DIR Folder------"
cd /home/$DIR/
rm -f *
echo -e -n "\nDone"

elif [ "$SERVER" -eq "3" ]; then

DIR=folder6

clear
echo -e -n "\n"
echo "------Entering $DIR Folder------"
cd /home/$DIR/
rm -f *
echo -e -n "\nDone"

elif [ "$SERVER" -eq "4" ]; then
echo -e "\nFinished\n"

else
echo -e "\nUnknown action selected\n"
fi


See the code for option 2 and 3 is the same except for the DIR it has to go into...I don't want that code there twice because it will make the script super long....

Thanks for the help in advance!

Last edited by Franklin52; 10-06-2011 at 03:27 AM.. Reason: Please use code tags and indentation
# 2  
Old 10-05-2011
you should use case statement.

Code:
case "$SERVER" in
1)
commands;
;;
2)
commands;
;;
*)
commands;
;;
esac

# 3  
Old 10-05-2011
Hi rdcwayx,

With the case command how does it know that option 2 and 3 are the same command just one thing different the variable declared $DIR?


Thanks,
Holyearth
# 4  
Old 10-05-2011
Define a function to save the typing.

Code:
clean ( )
{
clear
echo -e -n "\n"
echo "------Entering $DIR Folder------"
cd /home/$DIR/
rm -f *
echo -e -n "\nDone"
}

2)
DIR=folder3
clean
;;
3)
DIR=folder6
clean

# 5  
Old 10-05-2011
oh I got it...thats what I wanted was to define a function....I'll test that out...

Thanks!

---------- Post updated at 10:38 PM ---------- Previous update was at 09:47 PM ----------

Thanks, works like a charm now!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Different cmd to execute and based on some pattern parse it and then store result in xlx format

Hi I have a different requirement, I need to run some application on my device from a file app_name.txt one by one which is like this: /usr/apps/email /usr/apps/message /usr/apps/settings after each app while it is running I need to execute again one cmd like ps -ef |grep... (2 Replies)
Discussion started by: Sanjeev Roy
2 Replies

3. UNIX for Advanced & Expert Users

has no rc.local in /etc, how to auto run cmd in the boot process?

Hi I want to run some cmd before the linux boot up and I want to let it run before sshd service start, any helps? (1 Reply)
Discussion started by: yanglei_fage
1 Replies

4. UNIX for Dummies Questions & Answers

passing input into a cmd line that's waiting for a response

Hi, I am writing a little script to update a parameters on JMQ. however the JMQ requires a "y" confirmation to be input as part of the cmd I am running. However I want run this script to offline with no input from a user. it works if a I create a file with with just y in it and pass that in... (3 Replies)
Discussion started by: shropshirehobbi
3 Replies

5. UNIX for Dummies Questions & Answers

input URL into cmd with a alias

I want to run wget "URL" -SO /dev/null 2>&1 | grep "HTTP/\\|Age:\\|Last-Modified:" but I want a alias so I can just type mywget and the URL and it will put the url in the right place and give me the output that I want without having to type that over and over again.:wall: I am newbie to all... (2 Replies)
Discussion started by: splitradius
2 Replies

6. Shell Programming and Scripting

How to input the return value (1 or 0) ping cmd to a variable

Hi I would like to ask about my plan script I have this several workstation want to monitor and execute a command without logging it we use "rsh $host "<command>" i create csh script using foreach to loop my several workstation, my problem with the rsh command is if it encounter a... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. Shell Programming and Scripting

Unix cmd prompt how to get old cmd run?

Hi, I am using SunOS I want to serch my previous command from unix prompt (like on AIX we can search by ESC -k) how to get in SunOs urgent help require. (10 Replies)
Discussion started by: RahulJoshi
10 Replies

8. Programming

How to take input from cmd line into file

Hi, I want to be able to write a simple program that takes in input from the command line. I;m am at the level of getchar and putchar. Any examples would be a great help thanks. I intend/prefer also to use the pipe command | eg: input | file1 ---------- Post updated at 04:08 PM ----------... (4 Replies)
Discussion started by: metros
4 Replies

9. Shell Programming and Scripting

open terminal to run cmd using shell script

i want the shell script to open the terminal and in that terminal i want to run a command specified in the script... how can it be done... (2 Replies)
Discussion started by: chandrabhushan
2 Replies

10. Shell Programming and Scripting

ksh: cmd output to input of another script

I want to write a script in KSH that takes the output of one command and redisplays it. Something like: while true do read inpt date +"%I:%M:%S %p <-> $inpt" done and then some how get the output of the ping command to redirect to the input of this script. does that make sense? (2 Replies)
Discussion started by: IMTheNachoMan
2 Replies
Login or Register to Ask a Question