3
    echo "Enter path of backup file e.g /tmp/backup/etc.tar.gz : "
    read PATH    #input was /tmp/backup/etc.tar.gz
    echo "Enter directory: " 
    read DIR    #input was /root/testing

    sudo tar -zvxf "$PATH" -C "$DIR"

when I ran the script, it said that the command was not found. I tried using whatever kind of brackets for the variables but still not working. Any help?

However when I ran the command tar -zvxf /tmp/backup/etc.tar.gz -C /root/testing , it worked.

Patrick Kang
  • 55
  • 1
  • 1
  • 5
  • and/or the sudo-in-a-script problem: http://stackoverflow.com/questions/18364714/how-can-i-run-a-sudo-command-in-bash-script – adelphus Jul 25 '15 at 14:13
  • Yes tar is installed when i tried the command seperately it worked. It was the same without the sudo. – Patrick Kang Jul 25 '15 at 14:20
  • type `which tar` to find out the full path of tar and use the full path in the script (e.g. `sudo /usr/bin/tar ...`) if which returned `/usr/bin/tar` – Mario Zannone Jul 25 '15 at 14:22
  • 1
    The `sudo` issue revolves around `sudo` requiring the user to enter their password manually, when the script runs. As long as that's acceptable, there is no problem with `sudo` in a script. – John Bollinger Jul 25 '15 at 14:23
  • tried using /usr/bin/tar not working too. – Patrick Kang Jul 25 '15 at 14:28
  • Since PATH is a special environment variable, it may not be the best choice. Can you try some other name? – brm Jul 25 '15 at 14:48
  • Stack Overflow is a question and answer site for professional and enthusiast programmers. [Super User](http://superuser.com/tour) is a question and answer site for computer enthusiasts and power users. – Cyrus Jul 25 '15 at 14:51
  • OMG thanks! I changed the PATH to some other variable name. Thanks alot! Didn't realised that mistake. – Patrick Kang Jul 25 '15 at 15:10

1 Answers1

5

You're saving something into PATH which is what the shell will search to find the executables. So when you use that variable the shell can't find, say, tar because it is no longer in your search path. Use a different variable name.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • This is why lowercase names are recommended for shell variables -- all of the "special" environment variables are uppercase, so if you stick to lowercase you'll never accidentally step on one and break something. See [this previous question](http://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization). – Gordon Davisson Jul 25 '15 at 17:25