1

So I've been starting bash scripting, and I'm making a script that will autoinstall from source (so essentially it just compiles tarballs for you). I need it to change directories in order to go to a tarball. However, whenever I use this command

read path
cd $path

I always get the error tar-installer.sh: line 13: cd: ~: No such file or directory For anyone needing it, here's the full script...

#!/bin/bash
# This program auto-installs tarballs for you.
# I designed this for Linux noobies who don't
# know how to install tarballs. Or, it's for
# people like me who are just lazy, and don't
# want to put in the commands ourselves.

echo "Tar Installer v1.1"
echo "Gnu GPL v2.1"
echo -n "Path to tarball:"
read path
cd $path
echo -n "Please enter the file you wish to complile..."
read file
if  $file =="*.tar.gz"
then
    tar -xzf $file
   else
       $file =="*.tgz"
then
    tar -xzf $file
else
    $file =="*.tar.bz2"
then
    tar -xjf $file

The final part with the tarballs is still a work in progress. But the directory I used for cd path was ~/Downloads/

It's probably an easy fix, but I don't know how to fix it.

Keith Hughitt
  • 4,860
  • 5
  • 49
  • 54
bubbadoobop
  • 21
  • 1
  • 5
  • 1
    Issue appears to be related the read built-in function when accepting filepaths with "~/" in it. – Keith Hughitt Jul 02 '16 at 01:43
  • 3
    please use http://shellcheck.net and correct any errors found there before posting code (You should do this now). Thanks and good luck.. – shellter Jul 02 '16 at 03:17

2 Answers2

6

You need to replace the tilde ~ by the home path. This expansion fails if it is not performed directly.

 cd "${path/#~/$HOME}"

will replace ~ with your home directory using bash's replace ${value/search_term/replacement}.

You may also want to combine echo & read:

read -p "Path to tarball: " pathname

Also be careful when naming variables like path (PATH is your environment variable)

Jedi
  • 3,088
  • 2
  • 28
  • 47
  • 1
    How about the PATH `~other_user`? – Walter A Jul 02 '16 at 08:58
  • If your directory layout is regular Linux standard, adding a second substitution to replace any remaining leading `~` with `/home/` would seem like an obvious approach. – tripleee Jul 02 '16 at 11:22
-2

Because "cd" is build-in function of bash. So you should try to run your script like this:

 #source tar-installer.sh

Similar issue here:

[1]: https://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script

Community
  • 1
  • 1
Nam Pham
  • 316
  • 1
  • 6
  • No, this is not the problem reported. – anishsane Jul 02 '16 at 08:29
  • Why ? I do not understand. I tried to run exactly the script tar-installer.sh in Ubuntu 14:04 The first time I had error with "cd", but then there isn't error with "source" . So I think #source tar-installer.sh will solve the problem. – Nam Pham Jul 02 '16 at 16:05
  • The problem here is with tilde expansion. If `~` is already part of the variable, it will not get expanded. The question link you gave tackles a completely different problem. When you _execute_ a script as opposed to `source`ing it, a subshell is spawned. `cd` changes the working directory in that shell; working directory in parent remains unchanged. This behavior has nothing to do with the question asked here. – anishsane Jul 02 '16 at 17:53
  • 1
    Also, you should not `source` something that you don't know. `source` can change current shell's environment. You should be extremely careful with that. If you are the author of the script & have taken proper precautions so as not to corrupt the existing environment with unintended side effects, then only you should source it. – anishsane Jul 02 '16 at 17:58
  • Thank you for correcting me.I got it. I'm still new here,I will be more careful before posting a reply (: – Nam Pham Jul 13 '16 at 05:54