Multiple Variables for BASH script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple Variables for BASH script
# 1  
Old 04-05-2011
Multiple Variables for BASH script

Hello, I am new to the whole "scripting" thing. Below is the script that I have so far and where i need the Variables to go (VAR#)
Code:
#!/bin/bash
#Sample Script

VAR1=

echo "Choose an option: 1) Create a file. 2) Delete a file. 3) Move a file." 
read VAR1

case $VAR1 in
  1)
   echo "Pick new file name."
touch VAR2   
;;
  2)
   echo "Pick name of file to delete:"
rm VAR2
   ;;
  3)
   echo "Select Source File:"
read VAR2
   echo "Select Destination:"
read VAR3
   ;;  
  *)
   echo "Invalid option, please try again."
   ;;
esac


Im just not sure where in the script to include what VAR2 and VAR3 = would it be in the same area as VAR1= is located? please help if someone could.

store their choice in a variable named VAR2
store their choice in a variable named VAR3

---------- Post updated at 03:26 AM ---------- Previous update was at 03:06 AM ----------

oh also forgot to mention that I am using ubuntu 9.10

Last edited by Franklin52; 04-05-2011 at 04:22 AM.. Reason: Please use code tags
# 2  
Old 04-05-2011
You use 'read myVar' keyword to store the input from stdin (keyboard) into variable myVar. So you just need to read the input for each case. Now when you reference variable, that is when you are retrieving the information stored behind it, you use dollar sign, which you were missing in touch and rm commands.
Code:
#!/bin/bash
#Sample Script

VAR1=

echo "Choose an option: 1) Create a file. 2) Delete a file. 3) Move a file." 
read VAR1

case $VAR1 in
  1)
   echo "Pick new file name."
   read VAR2
   touch $VAR2   
;;
  2)
   echo "Pick name of file to delete:"
   read VAR2
   rm $VAR2
   ;;
  3)
   echo "Select Source File:"
read VAR2
   echo "Select Destination:"
read VAR3
echo "Source is $VAR2 and destination $VAR3"
   ;;  
  *)
   echo "Invalid option, please try again."
   ;;
esac


Last edited by mirni; 04-06-2011 at 03:50 AM..
This User Gave Thanks to mirni For This Post:
# 3  
Old 04-05-2011
Quote:
Originally Posted by mirni
You use 'read myVar' keyword to store the input from stdin (keyboard) into variable myVar. So you just need to read the input for each case. Now when you reference variable, that is when you are retrieving the information stored behind it, you use dollar sign, which you were missing in touch and rm commands.
Code:
#!/bin/bash
#Sample Script

VAR1=

echo "Choose an option: 1) Create a file. 2) Delete a file. 3) Move a file." 
read VAR1

case $VAR1 in
  1)
   echo "Pick new file name."
   read VAR2
   touch $VAR2   
;;
  2)
   echo "Pick name of file to delete:"
   read VAR2
   rm $VAR2
   ;;
  3)
   echo "Select Source File:"
read VAR2
   echo "Select Destination:"
read VAR3
echo "Source is $VAR2 and destination $VAR3"
   ;;  
  *)
   echo "Invalid option, please try again."
   ;;
esac

Please make sure your script is made executable only to the owner of the file,
Code:
chmod 744 script.sh

, since you don't really want all the world being able to execute 'rm' command...
thank you very much. i was hitting a Smilie and was starting to get frustrated.
# 4  
Old 04-05-2011
Quote:
Originally Posted by mirni
Please make sure your script is made executable only to the owner of the file,
Code:
chmod 744 script.sh

, since you don't really want all the world being able to execute 'rm' command...
Huh? Not sure what you're getting at here. All the world can usually execute the 'rm' command. Whether it's run from a script or an interactive prompt, the user still needs to have the appropriate privilege.

Regards,
Alister
# 5  
Old 04-06-2011
you're right Alister, I didn't think it through. Comment deleted :-\
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Connecting and changing variables in Bash script

#!/bin/bash X=$(</home/cogiz/computerhand.txt) # (3S 8C 2H 6D QC 8S 4H 5H) Y=$(</home/cogiz/topcardinplay.txt) # KS A=( "${Y::1}" ) B=( "${Y:1}" ) for e in ${X}; do if ]; then # searching for valid cards K,S or 8 ... (0 Replies)
Discussion started by: cogiz
0 Replies

2. Shell Programming and Scripting

Multiple bash variables passed into nawk

I have a file that has 2 fields called b_file: 11977 DAR.V3.20150209.1.CSV 3295 DAR.V3.20150209.1.CSV 1721 DAR.V2.20150210.1.CSV I need to search a sftplog using the field 1, but want to maintain the relationship between field 1 and 2. I am passing field 1 as a parameter in a bash loop. ... (14 Replies)
Discussion started by: smenago
14 Replies

3. Shell Programming and Scripting

Problem with variables and bash script

From the command line: dions-air:scripts dion$ ls -l /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX -rw-r--r-- 1 dion staff 157934 7 Jun 06:55 /Users/dion/Library/Application Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCXworks... (2 Replies)
Discussion started by: dionbl
2 Replies

4. Shell Programming and Scripting

'Dynamic' setting of variables in bash script

Hi all, I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea. n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done The error message is: bash: p1=line1: command not found bash: p2=line2: command... (8 Replies)
Discussion started by: jeppe83
8 Replies

5. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

6. UNIX for Advanced & Expert Users

Bash script with export variables

Hi all guys, how you can read in thread title, I'm deploying a bash script in which I have to export some variables inside it. But (I think you know) the export command works only inside the script and so, on exit command, the variables aren't set like I set inside the script. Consequently in... (8 Replies)
Discussion started by: idro
8 Replies

7. Shell Programming and Scripting

How to call multiple variables in bash !!

Hi all , I have a file with below data , bash#cat file.txt user1 amount1 status1 user2 amount2 status2 user3 amount3 status3 user4 amount4 status4 . . . Now i have a command to be executed with above values like below , ./errorcheck -u user1 -a amount1 -s status1 ... (3 Replies)
Discussion started by: gnanasekar_beem
3 Replies

8. Shell Programming and Scripting

problem using variables in bash script

I am using variable to give the location of the file I am using but I get error. Here is the code: LogFile=/tmp/log.email echo -e "could not close the service - error number $error \n" > $LogFile well this is not all the code but is enough because the problem start when I try to use the... (3 Replies)
Discussion started by: programAngel
3 Replies

9. Shell Programming and Scripting

Unable to change environment variables in bash script

Hello! For the moment some settings in my .bashrc contain the password of my company's firewall, which is not a good idea. I would like to use the string "PASSWORD" set in .bashrc and a script that changes all appearances of "PASSWORD" in the environment variables by the actual password (which... (4 Replies)
Discussion started by: markolopa
4 Replies

10. UNIX Desktop Questions & Answers

trying to create a script with multiple variables...

I have created a script that prompts the user to enter three variables that are seperated by a space as the delimiter. It then performs a command 3 seperate times for each variable entered. I want the script to llow the user to enter as many variables as they may like and the script to... (5 Replies)
Discussion started by: Italy87
5 Replies
Login or Register to Ask a Question