ssh bash script No such file or directory - bash

I am trying to run a script from /var/www/backups/scripts and when i try tell it to zip up a file i get the below error,
I can confirm that /var/www is the home dir and that the scripts work when ran manually though putty but just not though a script.
I'm using the below code to run the zip
#!/bin/bash
unset PATH
#USER VARS
HOMEDIR=~/
BACKUP_TARG_DIR=~/sites/backups/auto
BACKUP_TEMP_NAME=tempBackupFile.tar
BACKUP_TARG_FILE=/var/www/back
DATE=`/bin/date '+%Y-%m-%d'`
echo `/bin/pwd`;
tar -zcvf test.rar /var/www/backups/scripts/tryThis
#cd /var/www
#scp "tempBackupFile.tar" 217.41.51.14:~/testfile.rar;
#tar -zcvf $BACKUP_TEMP_NAME $BACKUP_TARG_FILE;
echo "SITE-"$DATE;
below is the output i get
/var/www/backups/scripts
./autoBackup.bash: line 18: tar: No such file or directory
SITE-2011-09-05
Any one have any ideas as this is killing me, all I can think of is its something to do with where the bash script is being run from.

Why do you unset PATH ?? No wonder bash cannot execute tar.

Check your /etc/ssh/sshd_config to make sure that you don't have a chroot directory set. If you do, you will need to either create a bin directory in the chroot directory and either copy or link the necessary binaries into that directory.
Or you could always comment that line out in the config.
Either way, restart sshd and test.

Related

Change directory in bash script in windows

How can I change my working directory in bash script in windows. I have
~dp0 = C:\test\docker\windows and I want to change my directory to C:\test\build
So it means 2 levels up and then int o build folder
Thanks
Since C:\ is mounted by default into /mnt/c this will work.
Create a .bashrc in your home path by following command:
echo "BUILDDIR=/mnt/c/test/build" >> ~/.bashrc;source ~/.bashrc
cd $BUILDDIR
# Do your work below for example ./configure.
./configure
On my system in Git bash the C:\ root is just /c/ and other dirs from there are whatever they are, so it would be cd /c/test/build/.
You could also still say cd ../../build/.
Good luck.
To change the directory using a bash script is just like you would using normal bash.
cd "C:/test/build"
echo "You're now in the folder, do what you will."
Save the file as .sh and it can be used as such.
Note, when navigation your folders using a bash script remember the directory you'll be starting from is always the home directory.

Make shell script change script location

I have currently put together a script to move files from one directory to another.
This has gone ok however I was wondering if there was a way via a shell script to get it to run from anywhere on the server e.g I give the script for someone to use on their server and they can put the script anywhere and it will run.
I know a workaround is to put the script in /usr/local/bin or usr/bin and you can run it from anywhere but that is not what I want.
Is there a way that my script will auto run from usr/local/bin regardless of if it is in /scripts for instance?
Please see my script below:
#!/bin/sh -x
mkdir -p /var/Alitest
echo "This is a test that I have created. This is to
see if the output is successful I normally do this manually but a script is required" > /var/Alitest/action.txt
sed -i 's/This is a test that I have created/The test has been successful/g' /var/Alitest/action.txt
chmod 744 /var/Alitest/action.txt
chown root:root Alitest/action.txt
mv /var/Alitest/action.txt /script/action.txt
Any help would be greatly appreciated :)
Also in my log output for the script the following error is shown:
sed: 1: "/var/Alitest/action.txt": invalid command code A
Any ideas?
You can make a soft link in /usr/local/bin for your script. Then it will be in everyone's path to be executed.
e.g. ln -s /script/yourscript.sh /usr/local/bin/yourscript.sh
After reviewing the matter further I have decided the the best way to action this is to add the folder destination e.g /scripts to my path.
This can be done by vimming into the .bashrc file on the server and adding the below line:
export PATH=/dir_name:$PATH
remember to refresh the profile in order for the changes to take effect.
You can check if this has been successful by running the below command:
echo $PATH
There is no way to get your script to do this however this would be better then a softlink as if you add it to $PATH then you do not have to go through the task of adding softlinks each time.
Thank you all for your help.
Kind Regards
Ali

tar -zxvf cannot unzip file

here's the problem:
First step
transfer the *.gz file to the remote host using ftp, the code below
open $IP
user nfc nfc123
bin
passive
cd /nfc/APPBAK
put $FULLNAME $DESTFILE
cd $DESTDIR
tar -zxvf $local_filename
quit
FTPIT
Second step
tar -zxvf $local_filename
but it says:
"?Invalid command. "
Should I change the mode of of the *.gz file first, any help will be appreciated.
You are trying to run the tar command inside FTP, as far as I can see, rather than in the shell after you've fetched the file with FTP. It is confusing since some shell commands, like cd, seem to work in FTP too, but the cd command actually attempts to change directory on the remote machine (you need lcd to change directory on the local machine).
Put simply, tar isn't a valid FTP command, which is why you get the ?Invalid command error.
try this one::
tar -xvf $local_filename
Please make sure that file has right permissions.

SCP says file has downloaded, but the file does not appear

I am using ssh to work on a remote server, however when I try to download a file using scp in this format:
scp name#website.com:somefile.zip ~/Desktop
It asks me for my password, and shows this:
somefile.zip 100% 6491 6.3KB/s 00:00
however, this file never appears on my desktop. Any help
I think that you are logging into the remote machine using ssh and then running the command on the remote machine. You should actually be running the command without logging into your remote server first.
You need to specify the file path
scp name#website.com:/path/to/somefile.zip ~/Desktop
~/Desktop should actually be a directory, not a file. I suggest that you do the following:
Remove the ~/Desktop file with rm ~/Desktop (or move it with mv if you want to keep its contents).
Create the directory with mkdir ~/Desktop.
Try again to scp the zip file.
BTW, when I need to copy files into directories, I usually put a slash after the directory to avoid such problems (in case I make a mistake), e.g. scp server:file ~/Desktop/; if the directory doesn't exist, I get an error instead of unwanted file creation.
You are doing this from a command line, and you have a working directory for that command line (on your local machine), this is the directory that your file will be downloaded to. The final argument in your command is only what you want the name of the file to be. So, first, change directory to where you want the file to land. I'm doing this from git bash on a Windows machine, so it looks like this:
cd C:\Users\myUserName\Downloads
Now that I have my working directory where I want the file to go:
scp -i 'c:\Users\myUserName\.ssh\AWSkeyfile.pem' ec2-user#xx.xxx.xxx.xxx:/home/ec2-user/IwantThisFile.tar IgotThisFile.tar
Or, in your case, (that is with the VERY strong password you must be using):
cd ~/Desktop
scp name#website.com:/path/to/somefile.zip somefile.zip

Bash script not working even when in my PATH

I created a simple bash script. The script works just fine.
When I run echo $PATH this prints my paths, I have:
/usr/local/sbin:/usr/local/bin/:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
So i moved my script to /usr/local/bin and ran chmod +x mybash.sh. I've even chmod 0777 mybash.sh
Now, when I run ./mybash.sh I just get the "No such file or directory"
Why is this happening and where's the best place to put my scripts
Once the script is in your path, you can run it just with the filename: mybash.sh rather than the path to the file: ./mybash.sh
./mybash.sh means run mybash.sh from the current folder. If you've moved mybash.sh to /usr/local/bin, then it's no longer in ./ (your current folder), so it can't find it.
Either move to /usr/local/bin to run it using ./mybash.sh or just use mybash.sh from any folder once you've moved it into a path folder.

Resources