The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
trouble using read to store values in variables from command output ProGrammar UNIX for Dummies Questions & Answers 2 01-09-2009 11:09 AM
environment variables sumsin High Level Programming 6 03-13-2006 11:17 AM
help..Environment variables... sekar sundaram UNIX for Dummies Questions & Answers 3 08-30-2005 04:35 AM
environment variables Esaia High Level Programming 2 02-20-2003 08:19 PM
what is the use of Environment variables indianguru UNIX for Dummies Questions & Answers 2 07-24-2001 10:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2009
kingpin2502 kingpin2502 is offline
Registered User
  
 

Join Date: May 2009
Posts: 11
Interpreting Logicals/Environment Variables using the read command

Hi All

I have something that from the outset seems really trivial but in practice is not quite working.

I have the following code sample in my shell script which illustrates the problem


Code:
echo "enter home directory"
read home
mkdir $home/newdir

The user then enters a logical $HOME in the prompt. This is what is displayed on the screen


Code:
enter home directory
$HOME
mkdir: cannot create directory `$HOME/newdir': No such file or directory

If I type the full name into the prompt, it works fine. But this isn't ideal as in the real script, the directory structure could be 10 directories deep. Typing the full path could lead to potential issues.

Is there some way for the read command to interpret environment variables?

Thanks
  #2 (permalink)  
Old 05-27-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
echo "enter home directory"
read home
eval mkdir $home/newdir

  #3 (permalink)  
Old 05-27-2009
kingpin2502 kingpin2502 is offline
Registered User
  
 

Join Date: May 2009
Posts: 11
Thank you, works a treat..
  #4 (permalink)  
Old 05-27-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,807
try eval

Code:
echo "enter home directory"
read home
eval mkdir $home/newdir

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0