cd is not working as expectd

cd is not working as expectd

am 18.04.2008 19:59:39 von Fox

My code is as follows:

----------------------------
#!/bin/bash
echo -n Where do you want to install ?
read new_install_dir

cd ${new_install_dir}
----------------------------

If my input is /home/fox/install - it works but if user inputs ~/
install it dump with the following error code:
../script/install: 6: ~/install: not found

how can i fix this error?

TIA

Re: cd is not working as expectd

am 18.04.2008 20:08:11 von cfajohnson

On 2008-04-18, Fox wrote:
> My code is as follows:
>
> ----------------------------
> #!/bin/bash
> echo -n Where do you want to install ?
> read new_install_dir
>
> cd ${new_install_dir}
> ----------------------------
>
> If my input is /home/fox/install - it works but if user inputs ~/
> install it dump with the following error code:
> ./script/install: 6: ~/install: not found
>
> how can i fix this error?

So long as there are no spaces in the input:

eval "cd $new_install_dir"


--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: cd is not working as expectd

am 18.04.2008 20:30:24 von Fox

On Apr 18, 11:08=A0pm, "Chris F.A. Johnson"
wrote:
> On 2008-04-18, Fox wrote:
> > My code is as follows:
>
> > ----------------------------
> > #!/bin/bash
> > echo -n Where do you want to install ?
> > read new_install_dir
>
> > cd ${new_install_dir}
> > ----------------------------
>
> > If my input is /home/fox/install - it works but if user inputs ~/
> > install it dump with the following error code:
> > ./script/install: 6: ~/install: not found
>
> > how can i fix this error?
>
> =A0 =A0So long as there are no spaces in the input:
>
> eval "cd $new_install_dir"
thanks a lot; it worked like a charm. I'd like to know why we need to
use eval command? It is not required for other operation such as
rmdir. Is this a cd specific or some sort of pitfall built into bash
shell.
>
> --
> =A0 =A0Chris F.A. Johnson, author =A0 =A0 =A0 hell/>
> =A0 =A0Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)=

> =A0  ===== My code in this post, if any, assumes the POSIX loc=
ale
> =A0  ===== and is released under the GNU General Public Licenc=
e

Re: cd is not working as expectd

am 18.04.2008 23:39:14 von Dan Mercer

"Fox" wrote in message
news:2624a906-7074-4cb6-a690-488d2182ed3e@a9g2000prl.googleg roups.com...
On Apr 18, 11:08 pm, "Chris F.A. Johnson"
wrote:
> On 2008-04-18, Fox wrote:
> > My code is as follows:
>
> > ----------------------------
> > #!/bin/bash
> > echo -n Where do you want to install ?
> > read new_install_dir
>
> > cd ${new_install_dir}
> > ----------------------------
>
> > If my input is /home/fox/install - it works but if user inputs ~/
> > install it dump with the following error code:
> > ./script/install: 6: ~/install: not found
>
> > how can i fix this error?
>
> So long as there are no spaces in the input:
>
> eval "cd $new_install_dir"
thanks a lot; it worked like a charm. I'd like to know why we need to
use eval command? It is not required for other operation such as
rmdir. Is this a cd specific or some sort of pitfall built into bash
shell.

Neither. It's the way the shell works. Tilde expansion occurs before
variable
expansion. So the shell sees a literal ~, not the HOME directory. Eval
causes the line to be reparsed after variable expansion.

Dan Mercer

>
> --
> Chris F.A. Johnson, author
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence