nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Interpreting ~ from user input

This is a discussion on Interpreting ~ from user input within the Shell scripting forums, part of the Development/Scripting category; Hi, I'm writing a script where the user can enter a directory name. For instance Code: #!/bin/sh echo Where do ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 08:56 PM
Junior Member
User
 
Join Date: Apr 2008
My distro: fedora 7
Posts: 2
Rep Power: 0
al001 is on a distinguished road
Default Interpreting ~ from user input

Hi,

I'm writing a script where the user can enter a directory name. For instance

Code:
#!/bin/sh

echo Where do you want to install the package? [$INSTALL_DIR]
read new_install_dir

cd $new_install_dir
Now if the user enters ~/tmp I want to go to the tmp directory in the user's home directory. With the code above the scripts sees the ~ as part of the name. Anyone knows how to solve this.

Thanks

a
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-18-2008, 11:57 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 578
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

Add eval before cd
Code:
#!/bin/sh

echo Where do you want to install the package? [$INSTALL_DIR]
read new_install_dir

eval "cd $new_install_dir"
echo "You are in $(pwd) directory"
eval used to read and concatenated together into a single command. This command is then read and executed by the shell. If there is a space between dirname it will not work e.g. "~/dir name"

HTH
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #3 (permalink)  
Old 04-21-2008, 08:27 PM
Junior Member
User
 
Join Date: Apr 2008
My distro: fedora 7
Posts: 2
Rep Power: 0
al001 is on a distinguished road
Default

That works. Thanks.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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

Similar Threads

Thread Thread Starter Forum Replies Last Post
Add normal user / FTP user usein NORMAL user(non-root) anilvrathod Shell scripting 0 12-07-2007 06:04 PM
how to enable a user and disabling a user narasimha.reddygnv Shell scripting 1 03-31-2007 12:31 AM
Shell script to supply or input a password passphrase kasimani Shell scripting 1 02-21-2007 09:21 AM
curses Problems with Non-Blocking input kgmf Coding in General 3 04-01-2005 01:49 PM
Program gets aborted on large input Ssk Linux software 7 03-14-2005 11:04 AM


All times are GMT +5.5. The time now is 04:37 AM.


Powered by vBulletin® Version 3.7.3 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36