shell scripts runs manually but not in crontab - linux

i have shell script to FTP a file from one server to another server called abc.sh below is the code inside it
#!/bin/bash
HOST='10.18.11.168'
USER='india'
PASS='India#2017'
FILE='inload.dat'
DIRECTORY='/inloading'
ftp -n $HOST <<END_SCRIPT
user $USER $PASS
cd $DIRECTORY
put $FILE
quit
END_SCRIPT
exit 0
i am able to run it using ./abc.sh and file also gets copied to remote server.
But when i use in crontab it is not ftp the file
below is the crontab entry
15 01 * * * /user/loader/abc.sh > /user/loader/error.log 2>&1
in the error.log it shows as local: inload.dat: No such file or directory

You're referencing the file inload.dat, which is relative to the directory the script is run from. When you run the script as ./abc.sh it looks for an inload.dat in the same directory.
Cron chooses which directory to run your script from when it executes (IIRC it generally defaults to /root or your HOME directory, but the specific location doesn't matter), and it's not necesarily the same directory that you're in when you run ./abc.sh.
The right solution is to make FILE and absolute path to the full location of inload.dat, so that your script no longer depends on being run from a certain directory in order to succeed.
There are other options, such as dynamically determining the directory the script lives in, but simply using absolute paths is generally the better choice.

Your local path is probably not what you want it to be. Before executing the ftp command, add a cd to the directory of where the file is located. Or have the full path name to the file $FILE.

Related

Move files between directories using shell script

I'm new to linux and shell script in general. I'm using a distribution of Debian on the WSL (Windows Subsystem for Linux). I'm trying to write a very simple bash script that will do the following:
create a file in a directory (child-directory-a)
move to the directory it is in
move the file to another directory (child-directory-b)
move to that directory
move the file to the parent directory
This is what I have so far (trying to keep things extremely simple for now)
touch child-directory-a/test.txt
cd child-directory-a
mv child-directory-a/test.txt home/username/child-directory-b
The first two lines work, but I keep getting a 'no such directory exists' error with the last one. The directory exists and that is the correct path (checked with pwd). I have also tried using different paths (i.e. child-directory-b, username/child-directory-b etc.) but to no avail. I can't understand why it's not working.
I've looked around forums/documentation and it seems that these commands should work as they do in the command line, but I can't seem to do the same in the script.
If anyone could explain what I'm missing/not understanding that would be brilliant.
Thank you.
You could create the script like this:
#!/bin/bash
# Store both child directories on variables that can be loaded
# as environment variables.
CHILD_A=${CHILD_A:=/home/username/child-directory-a}
CHILD_B=${CHILD_B:=/home/username/child-directory-b}
# Create both child folders. If they already exist nothing will
# be done, and no error will be emitted.
mkdir -p $CHILD_A
mkdir -p $CHILD_B
# Create a file inside CHILD_A
touch $CHILD_A/test.txt
# Change directory into CHILD_A
cd $CHILD_A
# Move the file to CHILD_B
mv $CHILD_A/test.txt $CHILD_B/test.txt
# Move to CHILD_B
cd $CHILD_B
# Move the file to the parent folder
mv $CHILD_B/test.txt ../test.txt
Take into account the following:
We make sure that all the folders exists and are created.
Use variables to avoid typos, with the ability to load dynamic values from environment variables.
Use absolute paths to simplify the movement between folders.
Use relative paths to move files relatives to where we are.
Another command that might be of use is pwd. It will tell you the directory you are on.
with your second line, you change the current directory to child-directory-a
so, in your third line there is an error because there is no subdirectory child-directory-a into subdirectory child-directory-a
Your third line should be instead :
mv test.txt ../child-directory-b
The point #4 of your script should be:
cd ../child-directory-b
(before that command the current directory is home/username/child-directory-a and after this command it becomes home/username/child-directory-b)
Then the point #5 and final point of your script should be:
mv test.txt ..
NB: you can display the current directory at any line of your script by using the command pwd (print working directory) in your script, it that helps
#!/bin/sh
# Variables
WORKING_DIR="/home/username/example scripts"
FILE_NAME="test file.txt"
DIR_A="${WORKING_DIR}/child-directory-a"
DIR_B="${WORKING_DIR}/child-directory-b"
# create a file in a directory (child-directory-a)
touch "${DIR_A}/${FILE_NAME}"
# move to the directory it is in
cd "${DIR_A}"
# move the file to another directory (child-directory-b)
mv "${FILE_NAME}" "${DIR_B}/"
# move to that directory
cd "${DIR_B}"
# move the file to the parent directory
mv "${FILE_NAME}" ../

FTP transfer issue

I am building FTP bash script to generate a .csv file and transfer from a Linux machine to another server, but i have problems because it tigers an error and the file is not transferred on the 2nd server. What could be the problem?
This is the error:
TEST: A file or directory in the path name does not exist.
Filename invalid
And it doesn't matter if i put the / before the TEST, it will trigger the same issue.
This is my script
HOST='ipadress'
USER='user'
PASSWD=''
TARGET='TEST'
#Paramenters
set -x
DATE=`date +%Y%m%d%H%M`
SQL=/home/sql_statement.sql
QUERYCMD=/home/report.sh
CSV=/home/csv/test_$DATE.csv
#Interogate the sql and put in the folder
$QUERYCMD ${SQL} ${CSV}
#Send the .csv file in the target folder
cd /home/csv
ftp -n $HOST <<EOF
quote USER $USER
quote PASS $PASSWD
lcd $TARGET
put $CSV $TARGET
quit
EOF
exit 0
does the symbol TARGET refer to a dir on the remote host?
ftp command lcd would change-dir on the local (client) side, while cd would change-dir on the remote (server) side; also for the remote side there's usually a designated ftp root dir, adjust any path in relation to that starting point; to confirm dir contents, you might add ftp commands ls and !ls on separate lines right after the PASS line

cpanel shell script on cloud server centos no such file or dir

I have a shell script copy_files.sh which I call once a day using a cron job.
However it has never worked I keep getting no such file or directory.
!#/bin/sh
for f in /home/site1/public_html/admin/data/*.csv
do
cp -v "$f" /home/site2/app/cron_jobs/data/"${f%.csv}".csv
done
I have checked via ssh that all paths are correct I have varified the path to /bin/sh using ls -l /bin/sh I have set perms and user and group to root for copy_files.sh I have disabled php open_basedir protection.
the shell script is in /home/site2/
Any ideas why I am still getting no such file or directory?
Is there anyway to check open_basedir protection is off that said considering the script is owned by root I don't see that being the problem unless it's executed as site2 user and not root?
Because of the way you use shell expansion, the variable in your for loop contains the absolute path to your files. Having the absolute path, means there is no need to use string manipulation (%) nor do you need to add the ".csv" to the filename, just get rid of it all together and provide the directory to which you're copying as your second argument to cp, see the example below.
#!/bin/sh
for f in /home/site1/public_html/admin/data/*.csv; do
cp -v "$f" /home/site2/app/cron_jobs/data
done

How do you run bash script as a command?

I have a bash script, which I use for configuration of different parameters in text files in my wireless access media server.
The script is located in one directory, and because I do all of configurations using putty, I have to either use the full path of the file or move to the directory that contains the file. I would like to avoid this.
Is it possible to save the bash script in or edit the bash script so that I can run it as command, for example as cp or ls commands?
The script needs to be executable, with:
chmod +x scriptname
(or similar).
Also, you want the script to be located in a directory that is in your PATH.
To see your PATH use:
echo $PATH
Your choices are: to move (or link) the file into one of those directories, or to add the directory it is in to your PATH.
You can add a directory to your PATH with:
PATH=$PATH:/name/of/my/directory
and if you do this in the file $HOME/.bashrc it will happen for each of your shell's automatically.
You can place a softlink to the script under /usr/local/bin (Should be in $PATH like John said)
ln -s /path/to/script /usr/local/bin/scriptname
This should do the trick.
You can write a minimal wrapper in your home directory:
#!/bin/bash
exec /yourpath/yourfile.extension
And run your child script with this command ./NameOfYourScript
update: Unix hawks will probably say the first solution is a no-brainer because of the additional admin work it will load on you. Agreed, but on your requirements, my solution works :)
Otherwise, you can use an alias; you will have to amend your .bashrc
alias menu='bash /yourpath/menuScript.sh'
Another way is to run it with:
/bin/bash /path/to/script
Then the file doesn't need to be executable.

Bash script failing to execute bash script

I'm trying to run a script that installs some files in a directory a user specifies. Once the user specifies the directory, I'd like to transfer the main file to that directory so it can perform so more tasks there before ultimately deleting itself once complete.
#prompt for directory in which to build project
read -p "Drag and drop the directory in which you'd like to build this project: "
echo "reply is $REPLY"
cp ./myScript.sh $REPLY
/bin/bash $REPLY/myScript.sh
I've got the script to execute the file from this question. I tried doing it with source $REPLY/myScript.sh as well as simply sh $REPLY/myScript.sh. I get the error /path/to/file/ is a directory
It must be that it doesn't known I'm trying to run myScript.sh, but I don't understand how I've given it a directory.
A likely cause is that drag-and-drop is putting whitespace after the directory name.
Thus:
/bin/bash $REPLY/myScript.sh
would be running
/bin/bash /path/to/directory /myScript.sh
A simple fix, if that's only a standard space, would be:
/bin/bash "${REPLY% }/myScript.sh"
You are missing the variable in read command so obiously it will fail as whatever you are reading is not getting stored. You can replace the read command as follows.
#prompt for directory in which to build project
read -p "Drag and drop the directory in which you'd like to build this project: " REPLY

Resources