Error: Tar command not found - bash

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.

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.

Related

mv: cannot stat [DIRECTORY/FILE]: no such file or directory

EDIT: DIR_trash="trash"
I wrote a function to move a file to current directory.
if [ "$1" == "-u" ]
then
if [ $# == 1 ]
then
echo "Something went wrong. Please make sure you're passing the name of the file/directory after '-u'."
else
if [ -f $DIR_trash/$2.zip ]
then
echo "$2.zip has been found in the trash."
cd
cd $DIR_trash
sed -i "/$2/d" $file7
mv -i /$DIR_trash/$2.zip .
unzip $2.zip
\rm $2.zip
cd
else
echo "$2.zip has not been found in the trash."
fi
fi
fi
As you can see, there is a line of code which says:
mv -i /DIR_trash/$2.zip .
So basically I'm trying to move a file that I passed in argument 2 to current directory, from trash. I always run this script from home directory, which does have trash directory. This is what I get when I run this:
Whenever I manually write this is in the Konsole (from home direcotry) it does work:
rm -u trash/d1 .
I'm out of ideas. Could anyone please help?
Let's say you run the script with the current directory being /some/where, and with the arguments -u and d1. I'll also assume that your home directory is /home/ninini. Let's look at where your script looks for files.
DIR_trash="trash"
if [ -f $DIR_trash/$2.zip ]
You check if /some/where/trash/d1.zip exists.
cd
cd $DIR_trash
Assuming both cd commands succeed, the current directory is now /home/ninini/trash.
mv -i /$DIR_trash/$2.zip .
You're saying to move /trash/d1.zip to the current directory, which is /home/ninini/trash.
Neither the source nor the destination make sense. The source /$DIR_trash doesn't make sense: why would you be looking for a directory called trash under the root directory? And the destination doesn't make sense since you just attempted to change to the trash directory, and now you're attempting to move a file out of the trash directory… into the trash directory.
I can't tell what the correct code is because you didn't say what the script is meant to do. You do say that you want to “to move a file to current directory”; then you must not change the current directory midway through the script! Assuming that the path $DIR_trash/$2.zip from the test command is the correct one, remove the cd commands and write
mv -i -- "$DIR_trash/$2.zip" .
Note that this moves the file from a directory called trash under the current directory. If this isn't what you wanted, you need to change the definition of DIR_trash. It should probably be an absolute path, perhaps
DIR_trash=~/trash
Note also that your script breaks on files containing whitespace and other special characters. Always put double quotes around variable substitutions: "$VAR", not $VAR. (Exception: when you know you need some effect that the double quotes prevent, and you understand why it's safe to leave them out.)

how to include path of a directory containing files inside bash script

I am writing a bash script that will get the user defined filenames and then use alsa to play that file:
#!/bin/bash
export PATH=$PATH:/home/pi/Documents/audio
read -p "Enter a filename: " filename
aplay $filename
Above is what I have tried after reading about how to include path in a script. But at the terminal prompt, after I do ./script.sh, it returns no such a file in the directory. I also tried source ./script.sh thinking that the environment is only changed in the subshell and it returns the same.
All I need is to include the path within this script and does not change the system path permanently.
I'd appreciate it if someone can point me to the right direction. Thank you!
PATH is used for finding programs, not data files. If you want $filename to be a file in the audio directory, you need to concatenate it explicitly.
#!/bin/bash
audiodir=/home/pi/Documents/audio
read -r -p "Enter a filename: " filename
aplay "$audiodir/$filename"
Try specifying the absolute path of the file that you're playing like this:
#!/bin/bash
AUDIO_PATH="/home/pi/Documents/audio"
read -p "Enter a filename: " filename
aplay "${AUDIO_PATH}/${filename}"

wget command not found while putting inside bash script

I have written following bash script:
#!/bin/sh
echo "Number of command line arguments : $#"
if [ $# == 0 ]; then
echo "Your command line contains no arguments"
declare -a arr=("xx.xx.xx.xx" "yy.yy.yy.yy")
else
declare -a arr=($1)
fi
for i in "${arr[#]}"
do
URL="https://"$i":8443"
echo "URL is $URL"
wget --no-check-certificate $URL/heapdump
done
It keeps failing with line 16: wget: command not found
I have found some related posts but could not figure out how to fix this. Thanks.
If you type a command (which is is not an internal command or a shell function), bash searches the directories mentioned in the variable PATH for a file of this name (wget in your case), which has the executable bit set for the user running the script. In your case, no suitable wget has been found.
Since it is unlikely that you do have a wget in your PATH without x-bit set, the most likely cause is that the PATH is lacking the directory where your wget lives.
You have two options: Extend the PATH, or explicitly prefix the wget line in your script with the correct path, i.e.
/here/is/my/wget --no-check-certificate $URL/heapdump
For getting path of executable files such as wget please use ‘which’ cmd:
which wget
Output:
/full/path/wget

How to store absolute path of back up files in log file using bash?

I am working on bash to create a back up system. My code is
#!/bin/bash
if [ ! -d "BackUp" ]
then
mkdir BackUp
fi
echo "enter number of access days you want to take for back up."
read days
bak="$(find . -mtime +$days)"
for file in $bak
do
mv $file BackUp
done
tar -cvf BackUp.tgz BackUp >> backUp.log
So, currently I am only taking log file from tar. so it does not prints the full path it only takes current working directory for text in log file.My last line of code takes up input for log file.
But the path stored is
.BackUp/foo1
.BackUp/foo2
.BackUp/foo3
instead i want it to be
home/ubuntu/Downloads/BackUp/foo1
home/ubuntu/Downloads/BackUp/foo2
home/ubuntu/Downloads/BackUp/foo3
You could store the absolute path in a variable and use it in the tar command:
BackUpDirFullPath=$(cd BackUp && pwd)
As command substitution invokes a subshell you are not leaving the current directory by executing cd.
Update:
In order to make -v output absolute paths (on Mac OS) I had to change to the root directory in a subshell and execute it from there ... something like that:
(cd / && tar -cvf /$OLDPWD/BackUp.tgz $BackUpDirFullPath)
This does output absolute paths ... in order to preserve the leading / you might try -P which preserves path names.

Directory name created with a dot ,using shell script

I am using Cygwin Terminal to run shell to execute shell scripts of my Windows 7 system.
I am creating directory , but it is getting created with a dot in name.
test.sh
#!/bin/bash
echo "Hello World"
temp=$(date '+%d%m%Y')
dirName="Test_$temp"
dirPath=/cygdrive/c/MyFolder/"$dirName"
echo "$dirName"
echo "$dirPath"
mkdir -m 777 $dirPath
on executing sh test.sh its creating folder as Test_26062015 while expectation is Test_26062015.Why are these 3 special charterers coming , how can I correct it
Double quote the $dirPath in the last command and add -p to ignore mkdir failures when the directory already exists: mkdir -m 777 -p "$dirPath". Besides this, take care when combining variables and strings: dirName="Test_${temp}" looks better than dirName="Test_$temp".
Also, use this for static analysis of your scripts.
UPDATE: By analyzing the debug output of sh -x, the issue appeared due to DOS-style line-endings in the OP's script. Converting the file to UNIX format solved the problem.

Resources