BASH Varible nesting and user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH Varible nesting and user input
# 1  
Old 11-18-2009
BASH Varible nesting and user input

Well, I think I've managed to take two different issues and conglomerate them into and embarrasing mess.

Code:
 
#!/bin/bash
# Set some variables
dir1=/path/that/isnt/variable/$variabledir/dir/
dir2=/path/that/isnt/variable/$variabledir/important/"$variabledir"-subdirectory/path/
 
echo "Gimme input, a whole lotta input"
echo ""
read variabledir
echo "Your input is: $variabledir"
echo $dir1
echo $dir2
#
exit 0

Why is this not working? Or, why is it working, and just not as expected? Reading throws me a null value for variabledir.

TIA!
# 2  
Old 11-18-2009
Quote:
Originally Posted by karlp
Well, I think I've managed to take two different issues and conglomerate them into and embarrasing mess.

Code:
 
#!/bin/bash
# Set some variables
dir1=/path/that/isnt/variable/$variabledir/dir/
dir2=/path/that/isnt/variable/$variabledir/important/"$variabledir"-subdirectory/path/


$dir1 and $dir2 will contain whatever the value of $variabledir is at the moment of assignment.
Quote:
Code:
echo "Gimme input, a whole lotta input"
echo ""
read variabledir
echo "Your input is: $variabledir"
echo $dir1
echo $dir2
#
exit 0

Why is this not working? Or, why is it working, and just not as expected? Reading throws me a null value for variabledir.
Code:
 
#!/bin/bash
# Set some variables
dir1=/path/that/isnt/variable/\$variabledir/dir/
dir2=/path/that/isnt/variable/\$variabledir/important/\$variabledir-subdirectory/path/
 
echo "Gimme input, a whole lotta input"
echo ""
read variabledir
echo "Your input is: $variabledir"
eval "echo $dir1;echo $dir2"
#
exit 0

# 3  
Old 11-18-2009
So, thanks, two questions...

1: The slashes prior to the dollar sign in dir1 and dir2, are those there for a reason?
2: Will removing the quotes from dir2's use of variabledir cause the -subdirectory to be appended, or would it be treated as a full and new variable with a dash in the name?

EDIT: 2 is not an issue, and I'm not sure why the slashes helped, but they did. Thanks a bunch!

Last edited by karlp; 11-18-2009 at 02:28 PM..
# 4  
Old 11-18-2009
Quote:
Originally Posted by karlp
So, thanks, two questions...

1: The slashes prior to the dollar sign in dir1 and dir2, are those there for a reason?

The slashes preserve the dollar signs as literal dollar signs.
Quote:
2: Will removing the quotes from dir2's use of variabledir cause the -subdirectory to be appended, or would it be treated as a full and new variable with a dash in the name?

The quotes don't do anything, so removing them doesn't affect anything.

(Quotes are only necessary in a variable assignment to preserve literal whitespace.)
# 5  
Old 11-18-2009
Thanks for the quick reply...

This is in fact a piece of a script for processing files associated with a particular application, and part of the script uses unzip to process files according to these variables.

Code:
unzip -n $dir2/$variabledir.zip

And I keep getting half of the string.

Code:
unzip:  cannot find or open /path/that/isnt/variable/$variabledir/dir/\$variabledir-subdirectory/path/$variabledir.zip

# 6  
Old 11-18-2009

Code:
eval "dir1=$dir1"
eval "dir2=$dir2"

# 7  
Old 11-18-2009
Code:
dir1=/path/that/isnt/variable/${variabledir}/dir/
dir2=/path/that/isnt/variable/$variabledir/important/${variabledir}-subdirectory/path/

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Nesting if statement in for loop

I have the basic command written in bash for element in 1 2 do if ]; then set el = "t" else set el = "p" fi done but i get the following error syntax error near unexpected token `for' ` for element in 1 2' What should i do differently? (3 Replies)
Discussion started by: ncwxpanther
3 Replies

2. Shell Programming and Scripting

BASH - read does not wait for user input in some circumstances

Hello. I am running 2 scripts : script_1 and script_2 These scripts are run as root Script 2 contains : #!/bin/bash # # ~/bin/script_2 # E_BAD_PARAM=115 # date2stamp () { date --date "$1" +%Y-%m-%d___%H:%M:%S } # USER_NAME=$1 NB_PARAM=$# PARAM0=$0 (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Using read to prompt for editable user input in Bash 3

Below is a simple script to prompt for user input while suggesting an editable default value at the prompt: shortname=user1 read -e -i $shortname -p "Please enter the username you would like to add: " input USERNAME="${input:-$shortname}" Please enter the username you would like to add:... (3 Replies)
Discussion started by: woodson2
3 Replies

4. Shell Programming and Scripting

[bash] how is proper way to validate user input

hi all, i have a script that need user input provide all variables that needed to complete a job. this is my current script: echo "type file source and it full path :" read INPUTFILE if || ; then echo "ERROR: you didn't enter a file source or file source is not... (2 Replies)
Discussion started by: makan
2 Replies

5. Shell Programming and Scripting

Help with Bash user input

I am starting to learn how to use bash and I would like the script to do the following: Asks the user for his/her name Asks the user for one number Asks the user for another number Then it adds the two numbers, Also multiply the two numbers I have the part where you get the name, and I... (3 Replies)
Discussion started by: boyboy1212
3 Replies

6. Shell Programming and Scripting

Bash user input

Hi all, I currently have a script which uses read -p for user interaction. e.g. read -p "New user? " user Is it possible to have it so if the user enters nothing and just presses return it can resort to a specified value instead? Thanks! :) (5 Replies)
Discussion started by: JayC89
5 Replies

7. UNIX for Dummies Questions & Answers

BASH validate user input

Hey, im trying to validate a user input and need some help. The input needs to be just a single letter. Im using a case to so this eg: read answer case $answer in *) echo "OK" ;; *) echo "This is a number" read answer ;; *) echo... (2 Replies)
Discussion started by: 602chrislys
2 Replies

8. Shell Programming and Scripting

BASH: Any Way to Get User Input Without Requiring Them to Hit the Enter Key?

I'm working on making a menu system on an HP-UX box with Bash on it. The old menu system presents the users with a standard text menu with numbers to make selections. I'm re-working the system and I would like to provide something more akin to iterative search in Emacs. I have a list of 28... (2 Replies)
Discussion started by: deckard
2 Replies

9. Shell Programming and Scripting

Bash : how do i check the user input and make sure is only chracter or only number ?

Bash : how do i check the user input and make sure is only character or only number ? (7 Replies)
Discussion started by: CheeSen
7 Replies

10. Shell Programming and Scripting

Using varible/varible substitution in Perl/sed Search & Replace

Hi, I have a program that searches for a particular string patten. I am however having difficulty passing the varible $i (in a loop) as the string pattern to replace. Using either perl or sed search and replace statements, I get the same kinda result. For example, using the perl: for i in... (3 Replies)
Discussion started by: Breen
3 Replies
Login or Register to Ask a Question