Shell Script Error : too many arguments in input line - bash

There is a file lets say List.txt and it contains the information of files which has to be copy from server to machine and then delete.
When shell scripts executes, it reads the List.txt file, store the files names in a variable, copy files from server to machine and then delete the server's files.
if file List.txt contains upto 16 file names then it's working fine.
in case of List.txt contains more than 16 files it gives error.
Error : too many arguments in input line
Any idea on it.
#Path of Log file
logdir="$HOME/log"
ftp="/usr/bin/ftp"
IP="192.168.1.199"
#User ID to login into remote server
user="tux"
#Password to login in remote server
pass="tux"
#Mention the path where the files will come in the current server
getlocaldir="/home/local/in"
#Mention the path where the FILEs are to FTP(get) in the remote server
getremotedir="/home/remote/out"
#Mention type of data transfer "asc" or "bin"
type="asc"
#date of FTP - Current date
dd=`date +%d`
# Creating a log file with datestamp
log="$logdir/wns_ft.log"
host=`hostname`
rc=0
boj=`date`
#current business date
BUSINESS_DATE=$HOME/file/businessdate
#==================
# Get Business Date
#==================
if [ ! -s $BUSINESS_DATE ]
then
echo -e "Get system date as the business date" >>$log
BDATE=`date +%y%m%d`
else
BDATE=`cut -c3-8 $BUSINESS_DATE` 2>>$log
fi
echo -e "Current business date: $BDATE" >>$log
#a text file contains the name of all file which has to be copied
bingoFileName=List.txt
dogetftp ()
{
LOCAL=$1
REMOTE=$2
echo "================================================"
echo "== Receiving Files From $IP =="
echo "================================================"
exec 4>&1
ftp -nv >&4 2>&1 |&
pid2=$!
print -p open $IP
print -p user $user $pass
print -p asc
print -p lcd $LOCAL
print -p cd $REMOTE
print -p pwd
print -p prompt
print -p mget $fileNamesListStr
print -p mdelete $fileNamesListStr $bingoFileName
print -p bye
wait $pid2
}
# method to get the bingo file containing plu-files-names to download
dogetbingo ()
{
LOCAL=$1
REMOTE=$2
echo "================================================"
echo "== Receiving Bingo file From $IP =="
echo "================================================"
exec 4>&1
ftp -nv >&4 2>&1 |&
pid2=$!
print -p open $IP
print -p user $user $pass
print -p asc
print -p lcd $LOCAL
print -p cd $REMOTE
print -p pwd
print -p prompt
print -p mget $bingoFileName
print -p bye
wait $pid2
}
# Method to read content of bingo file and creates file name string.
doreadBingo () {
echo "================================================"
echo "= Begin Reading Bingo File ="
echo "================================================"
LOCAL=$1
cd $LOCAL
if test -f $bingoFileName # Check if the bingo file exists
then
while read -r line
do
fileNamesListStr="$fileNamesListStr $line"
done < $bingoFileName
fi
echo "Files to download: $fileNamesListStr "
echo "================================================"
echo "= End Reading Bingo File ="
echo "================================================"
}
docheckget () {
LOCAL=$1
REMOTE=$2
DNLD_SUCCESS=`grep 'local: PLU' $log`
echo "SUCCESS: "$DNLD_SUCCESS
if [ "X$DNLD_SUCCESS" == "X" ]
then
NOT_FND_FILE_NAME="PLUNOTFOUND`date +%Y%m%d`.txt"
touch $LOCAL/$NOT_FND_FILE_NAME
echo "================================================"
echo "== Sending PLUNOTFOUND File to $IP =="
echo "================================================"
exec 4>&1
ftp -nv >&4 2>&1 |&
pid2=$!
print -p open $IP
print -p user $user $pass
print -p asc
print -p lcd $LOCAL
print -p cd $REMOTE
print -p pwd
print -p prompt
print -p put $NOT_FND_FILE_NAME
print -p bye
wait $pid2
rm $LOCAL/$NOT_FND_FILE_NAME
fi
}
case $1 in
mget)
#cd $localdir
exec 1>$log 2>&1
echo "---------------------------------------------------"
echo " Transfer bingo file "
echo "---------------------------------------------------"
dogetbingo $getlocaldir $getremotedir
echo "---------------------------------------------------"
echo " Read bingo file for file names to download "
echo "---------------------------------------------------"
doreadBingo $getlocaldir
echo "---------------------------------------------------"
echo " Begin FTP Session "
echo "---------------------------------------------------"
if [ "X$fileNamesListStr" != "X" ]
then
dogetftp $getlocaldir $getremotedir
docheckget $getlocaldir $getremotedir
else
echo "Nothing in Bingo file to download"
fi
echo "---------------------------------------------------"
echo " End FTP Session "
echo "---------------------------------------------------" ;;
esac
exit 0

I suspect this is a limitation with mget within your FTP client (or at the server end, possibly).
Best advice is to actually create one get line per file in $fileNamesListStr within the dogetftp() function.
For example:
dogetftp ()
{
LOCAL=$1
REMOTE=$2
echo "================================================"
echo "== Receiving Files From $IP =="
echo "================================================"
exec 4>&1
ftp -nv >&4 2>&1 |&
pid2=$!
print -p open $IP
print -p user $user $pass
print -p asc
print -p lcd $LOCAL
print -p cd $REMOTE
print -p pwd
print -p prompt
### Begin change here.
for fspec in $fileNamesListStr ; do
print -p get $fspec
print -p delete $fspec
done
print -p delete $bingoFileName
### End change here.
print -p bye
wait $pid2
}

Related

Command not found in elif comparison > bash

For some reason commands are not running after the first if statement.
"/usr/local//backup.sh: sleep: not found" for example..
The first if statement goes like:
if [ "$command" == "start -c" ]; then
echo -e "${ORANGE}Starting website backup...${E}"
sleep 1
DT=`date +%Y-%m-%d`
echo -e "${ORANGE}Started backup at $DT ${E}"
sleep 1
tar -zcvf web-$DT.tar.gz /usr/local/www/nginx
echo -e "${ORANGE}Compatced files, moving to backup directory${E}"
sleep 1
mv web-$DT.tar.gz $PATH/web-$DT.tar.gz
echo -e "${CYAN}Moved to backups ($PATH/web-$DT.tar.gz)\nDo you want to upload to ftp?${E}\n${CYAN} 'y' or 'n'?${E}"
read answer
if [ "$answer" == "y" ]; then
echo -e "${ORANGE}Uploading to ftp server...${E}"
sleep 1
if ftp -in -u ftp://$USER:$PASS#$HOST/Backups/f $PATH/web-$DT.tar.gz; then
echo -e "${GREEN}Uploaded! Bye!${E}"
else
echo -e "${ORANGE}Couldn't upload with ftp command, trying with curl...${E}"
sleep 1
curl -T "$PATH/web-$DT.tar.gz" -u $USER:$PASS ftp://$HOST/Backups/f/
echo -e "${GREEN}Uploaded! Bye!${E}"
fi
else
echo -e "${GREEN}Bye!${E}"
fi
Everything works fine inside this statement, however, after the elif compassion it just doesn't.
Here's the problematic code:
elif [ "$command" == "start -d" ]; then
echo -e "${ORANGE}Starting database backup...${E}"
sleep 1
DT=`date +%Y-%m-%d`
echo -e "${ORANGE}Started backup at $DT ${E}"
sleep 1
umask 177
echo -e "${ORANGE}Dumping database 'account'\n${E}"
mysqldump --user=$DBUSER --password=$DBPASS --host=$HOST account > $PATH/account-$DT.sql
sleep 1
echo -e "${ORANGE}Dumping database 'game'\n${E}"
mysqldump --user=$DBUSER --password=$DBPASS --host=$HOST game > $PATH/game-$DT.sql
sleep 1
echo -e "${ORANGE}Dumping database 'forum'\n${E}"
mysqldump --user=$DBUSER --password=$DBPASS --host=$HOST forum > $PATH/forum-$DT.sql
sleep 1
echo -e "${CYAN}Moved to backups ($PATH/<database-date>.sql)\nDo you want to upload to ftp?${E}\n${CYAN} 'y' or 'n'?${E}"
read answer
if [ "$answer" == "y" ]; then
echo -e "${ORANGE}Uploading to ftp server...${E}"
sleep 1
curl -T "$PATH/account-$DT.sql" -u $USER:$PASS ftp://$HOST/Backups/f/Databases/
curl -T "$PATH/game-$DT.sql" -u $USER:$PASS ftp://$HOST/Backups/f/Databases/
curl -T "$PATH/forum-$DT.sql" -u $USER:$PASS ftp://$HOST/Backups/f/Databases/
sleep 1
echo -e "${GREEN}Uploaded! Bye!${E}"
else
echo -e "${GREEN}Bye!${E}"
fi
and then I just ended it..
else
echo -e "${RED}Sorry! Not found${E}"
fi
As you can see there's spaces in the if statements and all of that, so what's wrong with this??
I've the #!/bin/bash up top as well, all the variables exist too.
You seem to have overwritten the value of the PATH variable at some point in your code. The shell needs that variable in order to know where to find programs like sleep (Hence the "not found" error). Name your path variable something else.

bash upload to sftp and moving files. looping around

Im somewhat beginner in bash scripting. I have here part of script that uploades files to sftp server. Then it shoud move files to other directory.
Else echo info about no files in source_dir. Problem is that its not moving files and is looping back to sftp login after files are already uploaded.
cd /dest_dir/
for file5 in *.test
do
if test -f $file5
then
echo 'some text.'
((
echo "
cd /dest_dir/
lcd /source_dir/
mput *.test
exit
"
) | sftp "user"#"host")
sleep 3
mv /source_dir/$file5 /source_dir/test/
else
echo -e 'some text'
fi
There is normally no reason to check that the files you are looping over actually exist. If a file could disappear between the for and the test, it could just as well disappear between the test and the echo instead, in which case you end up referring to a file which no longer exists anyway.
#!/bin/bash
shopt -s nullglob # bash-only feature
cd /dest_dir/
(#echo "cd /dest_dir/" # no use, you already did above
echo "lcd /source_dir/"
for file in *.test; do
echo put "$file"
done
echo "exit" ) | sftp "user"#"host"
mv *.test /source_dir/test/
This opens a single scp connection for all the files. It has the distinct disadvantage that if one of the files fails for some reason, the script doesn't catch that.
Apart from the shopt -s nullglob this should be portable to Bourne shell in general.
thank you for tips. I made it: with loging:
SFTP_HOST="host"
SFTP_LOGIN="USER"
cd /path/
mylog=/path/sftp_session_$(date "+%Y_%m_%d").log
echo "$(date "+%Y-%m-%d %H:%M:%S") - text" >> $mylog
let licz5=`ls|wc -l`
let jest_plik=0
for file5 in *.test
do
if test -f /patch_source1/$file5
then
let jest_plik=1
echo "$(date "+%H:%M:%S") - transfer pliku "$file5" na serwer "$SFTP_HOST >> $mylog
fi
if test -f /source_patch2/$file6
then
echo "$(date "+%H:%M:%S") - transfer pliku "$file6" na serwer "$SFTP_HOST >> $mylog
fi
if test -f /source_patch3/$file7
then
echo "$(date "+%H:%M:%S") - transfer pliku "$file7" na serwer "$SFTP_HOST >> $mylog
fi
if test -f /source_patch4/$file8
then
echo "$(date "+%H:%M:%S") - transfer pliku "$file8" na serwer "$SFTP_HOST >> $mylog
fi
done
if test $jest_plik -eq 1
then
echo 'text.'
(echo 'cd /dest_dir/
lcd /source_p_1/
mput *.test
lcd /source_p_2/
mput *.test
lcd /source_p_3/
mput *.test
lcd /source_p_4/
mput *.test
exit') | sftp $SFTP_LOGIN#$SFTP_HOST
echo "$(date "+%H:%M:%S") - send ok." >> $mylog
else
echo -e 'text'
fi

Bash: check if txt files exist in a dir and if yes, process them with aria2c

OK, so, I'm trying to create a script that...
a. Will look in a folder where I drop my txt notes, and if any of them starts with "a2c_" it will recognize it as an "aria2c download list". Which brings as to...
b. Will pass the first of the matching files to aria2c, together with a directory name similar to the txt file, so that it will download each URL found in the txt to the same-named directory.
I've ended up with this..:
#!/bin/bash
#PARAMETERS: _______________________________
workingdir="/home/username/downloads/"
#___________________________________________
echo Working dir is: $workingdir
mkdir -p $workingdir
echo "Making dir"
cd $workingdir
if [ -f a2c_* ]
then
echo "Found files"
mkdir -p !a2c_downloaded
echo "Making !a2c_downloaded dir"
counter=0
echo "Set counter to 0"
for f in a2c_*.txt
do
counter=$((counter+1))
echo Download List File is: $f
echo $counter file processing.
tempfile=${f%%.*}
tempfile="`echo "$tempfile" | sed ' s/a2c_//' `"
downdir=$tempfile
echo Download Dir is: $downdir
mkdir -p $downdir
echo ___________________________________________
echo $endfilename
aria2c --auto-file-renaming -i $f -d $downdir --force-sequential
echo "Will download $f to $downdir"
sleep 5
mv $f !a2c_downloaded/
done
else
echo "No files found"
fi
...that worked when I tested it. Today, one day later, I throw some a2c_*.txt files in the dir and I'm met with "unexpected operator" errors. Any ideas? And is there an easier way to accomplish what I'm trying to do?
Thanks.
_UPDATED: _________________________________
#!/bin/bash
#PARAMETERS: _______________________________
workingdir="/home/username/downloads/"
MatchPattern="a2c_*"
#___________________________________________
echo Working dir is: $workingdir
mkdir -p $workingdir
echo "Making dir"
cd $workingdir
#if [ -f a2c_* ];
#if [ find . -maxdepth 1 -type f -name "a2c_*.txt" 2>/dev/null | grep "a2c_*" ]
#if [ -f a2c_* ]
#if [ "$?" = "0" ];
echo "Match Pattern set as $MatchPattern"
echo "Now looking in $workingdir for $MatchPattern"
echo "Manual list:"
echo "_________________________________________"
MatchList=$(ls -1 "$MatchPattern")
echo "$MatchList"
echo "_________________________________________"
if ls -1 $MatchPattern >/dev/null 2>&1
then
echo "Found files"
mkdir -p !a2c_downloaded
echo "Making !a2c_downloaded dir"
counter=0
echo "Set counter to 0"
for f in a2c_*.txt
do
counter=$((counter+1))
echo "Download List File is: $f"
echo $counter file processing.
tempfile=${f%%.*}
tempfile="`echo "$tempfile" | sed ' s/a2c_//' `"
downdir=$tempfile
echo "Download Dir is: $downdir"
mkdir -p "$downdir"
echo ___________________________________________
echo "$endfilename"
aria2c --auto-file-renaming -i "$f" -d "$downdir" --force-sequential
echo "Will download $f to $downdir"
sleep 5
mv "$f" !a2c_downloaded/
done
else
echo "No files found"
fi
...The above works, after some fixes http://www.shellcheck.net/ told me to do. Problem is, it advises me to use double quotes in line:
if ls -1 $MatchPattern >/dev/null 2>&1
like
if ls -1 "$MatchPattern" >/dev/null 2>&1
..."Double quote to prevent globbing and word splitting.", but when I do, the script stops working for me. Should I leave it as it is? It seems to be working fine - for now.
Instead of
if [ -f a2c_* ]
then
You can try this:
file_exists() {
for _i do
[ -f "$_i" ] && break
done
}
and then
if file_exists a2c_*
then

Bash Variable losing its value - strange

notice the FTP_PUT_RETVAL. There are two echo commands, the first one shows a value of '0'. The second one shows no value at all. I am running this inside a nightly cron. BASH is GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
My script tells me that the ftp upload was unsuccessful, because FTP_PUT_RETVAL contains no value!
Why is this occuring? How can I change this? What have I done wrong?
#!/bin/sh
#
# Filename: rh5-manager-cron.sh
#
# Purpose: Runs backup.cron, and FTP uploads backup to Linux4
#
# Usage: the program is run with no arguments. This program is
# intended to run from manager cron, and redirection is handled inside the cron
# e.g. 00 1 * * * /u2/app/lbin/rh5-manager-cron.sh > /tmp/log/rh5-manager-cron.log 2>&1
trap signalCaught ABRT EXIT HUP INT PIPE TERM QUIT
signalCaught () {
for file in $FTP_LOG_FILE $TMPF; do
$BIN_HOME/rm_file $TMPF
done
exit
}
if [[ ! -r /u2/conf/ctrl/lettus.sh || ! -r /u2/app/lbin/fsh_sh_lib ]]; then
printf "Cannot source environment.\n" >&2
exit 1
fi
. /u2/conf/ctrl/lettus.sh
. /u2/app/lbin/fsh_sh_lib
#-------------------------------------------------------
# Variables
#-------------------------------------------------------
PRG=${0##*/}
FTPUSER=dummy
FTPPASS=dummy
FTPHOST=192.168.0.3
BACKUPDIR=/backup
FTPBACKUPNAME=Redhat5Backup.tgz
FTPBACKUPFILE=$BACKUPDIR/$FTPBACKUPNAME
LOGDIR=/tmp/log
FTP_LOG_FILE=$LOGDIR/Redhat5FTP.log
SENDLOGS=$C/sendlogs.dat
ZOOT_XML=$C/zoot.xml
TMPF=`mktemp` || exit 1
#-------------------------------------------------------
# Sanity
#-------------------------------------------------------
if ! isValidUser root manager; then
printf "$PRG: Must be run as user 'root' or user 'manager'.\n" >&2
exit 1
fi
if [[ ! -d $BACKUPDIR ]]; then
logger "$PRG: $BACKUPDIR: Not a valid directory" >&2
exit 1
fi
if [[ ! -d $LOGDIR ]]; then
logger "$PRG: $LOGDIR: Not a valid directory" >&2
exit 1
fi
if [[ ! -r $SENDLOGS || ! -r $ZOOT_XML ]]; then
logger "$PRG: E-mail: Files are not readable: $SENDLOGS, $ZOOT_XML" >&2
exit 1
fi
if ! which lftp >/dev/null 2>&1; then
logger "$PRG: lftp: command not found" >&2
exit 1
fi
# e-mail
EMAIL_SUBJECT="Redhat5 FTP PUT"
EMAIL_TO=$(email_to $SENDLOGS)
EMAIL_FROM=$(email_from $ZOOT_XML)
# ftp binary
LFTP_BIN=$(which lftp 2>/dev/null)
#-------------------------------------------------------
# Functions
#-------------------------------------------------------
# calls lftp to upload a file to server non-interactively
ftp_put() {
$LFTP_BIN $LFTP_DEBUG -u ${FTPUSER},${FTPPASS} $FTPHOST <<-EOF
put $FTPBACKUPFILE
pwd
ls $FTPBACKUPNAME
quit
EOF
#^^^^^^^^^^^ leave the above indentation alone
}
main() {
# Backup, and send manager logs
logger "Running backup.cron..."
echo
backup.cron
BACKUP_CRON_RETVAL=$?
logger "Running fsh_sendlogs..."
echo
$SH_HOME/fsh_sendlogs 1
# show ls listing
logger "Here comes a directory listing..."
echo
/bin/ls -l /backup/Redhat5Backup.tgz
echo
# ftp upload
logger "Running ftp upload..."
echo
ftp_put
FTP_PUT_RETVAL=$?
echo " -- debug: Line ${LINENO:-}: FTP_PUT_RETVAL=${FTP_PUT_RETVAL:-}"
}
checkSuccess() {
if [[ "$BACKUP_CRON_RETVAL" -eq 0 ]]; then
echo "Backup was successful."
echo
echo "*********************************"
echo " OK: Backup was successful."
echo "*********************************"
echo
else
echo "ERROR: Backup FAILED! "
echo
echo "*********************************"
echo " ERROR: Backup FAILED! "
echo "*********************************"
echo
fi
echo " -- debug: Line ${LINENO:-}: FTP_PUT_RETVAL=${FTP_PUT_RETVAL:-}"
if [ "$FTP_PUT_RETVAL" -eq 0 ]; then
echo "lftp: ftp file upload complete."
echo
echo "*********************************"
echo " OK: ftp file upload complete."
echo "*********************************"
echo
else
echo "lftp: error ftp file upload not successful."
echo
echo "*********************************"
echo " ERROR: file upload"
echo " NOT successful."
echo "*********************************"
echo
fi
}
email_logs() {
if [[ -z "$EMAIL_FROM" || -z $EMAIL_TO || -z $EMAIL_SUBJECT || ! -r $FTP_LOG_FILE ]]; then
logger "$PRG: One of the variables is not correctly configured" >&2
exit 1
fi
fsh_mail -r "${EMAIL_TO}" -F "${EMAIL_FROM}" -s "${EMAIL_SUBJECT}" -t "${FTP_LOG_FILE}"
return
}
#----------------------------------------------------------------------
# Main Program
#----------------------------------------------------------------------
main | tee $FTP_LOG_FILE
checkSuccess | tee -a $FTP_LOG_FILE $TMPF
cat $FTP_LOG_FILE >> $TMPF
# E-mail ftp log file
logger "Emailing ftp logfile"
echo
email_logs
#eof
The problem is that you are using:
main | tee $FTP_LOG_FILE
checkSuccess | tee -a $FTP_LOG_FILE $TMPF
Because of the piping, the work done in main is done in a sub-shell, and a sub-shell cannot set variables in the parent shell. So, when checkSuccess (which is also run in a sub-shell) goes to look at the results, it sees what was in the parent shell (nothing), rather than what was set in the main function in a sub-shell.
If you dropped the piping, it would start to work.
More reliably, put the call to checkSuccess into the main function. Or use one or the other of these two notations:
{ main; checkSuccess; } | tee $FTP_LOG_FILE $TMPF
( main; checkSuccess ) | tee $FTP_LOG_FILE $TMPF
Note that the { ... } notation requires a semi-colon after the second function which the ( ... ) notation does not require. However, there is one less process involved in the { ... } mechanism, not that it is going to make any measurable difference to the overall performance of this (the FTP time will dominate).

how to use a nested if and perform correct file checks in unix

Need some shell scripting help, especially with my if-then-else logic. I want to combine both conditions, but not sure if the file checks will work the same? Should I be doing something like a nested if?? My script uses the if statements to do file checks to see if they exist, then do something..
There is probably a better way to do file checks part too.
Any help, critique would be appreciated. Thanks.
Here's my code, it sort of works.
if [ $# != 1 ]; then
echo "Usage: getlogs.sh <remote-host>" 2>&1
exit 1
fi
#Declare variables
STAMP=`date '+%Y%m%d-%H:%M'`
REMOTE_MYCNF=/var/log/mysoft/mysoft.log
REMOTE_GZ=/var/log/mysoft/mysoft.log.1.gz
REMOTE_DIR=/var/log/mysoft/
BACKUP_DIR=/home/mysql/dev/logs/
NEWLOG="foo-temp.log"
export REMOTE_MYCNF STAMP SHORTNAME
export REMOTE_DIR REMOTE_GZ
#Copy file over
echo "START..." 2>&1
test -f $BACKUP_DIR$1.mysoft.log
if [ $? = 0 ]; then
echo "Local log file exists, clean up for new copy..." 2>&1
/bin/rm $BACKUP_DIR$1.mysoft.log
exit 0
else
echo "File does not exist, getting a new copy..." 2>&1
fi
echo "Checking remotely in $1 for foo logfile $REMOTE_MYCNF $STAMP" 2>&1
if [ ! -f $REMOTE_MYCNF ]; then
echo "File exists remotely, creating new logfile and copy here...." 2>&1
ssh $1 "zcat $REMOTE_GZ >> $REMOTE_DIR$NEWLOG"
ssh $1 "cat $REMOTE_MYCNF >> $REMOTE_DIR$NEWLOG"
/usr/bin/scp $1:$REMOTE_DIR$NEWLOG $BACKUP_DIR$1.mysoft.log
echo "end remote copy" 2>&1
echo "Cleaning up remote files" 2>&1
ssh $1 "rm $REMOTE_DIR$NEWLOG"
exit 0
else
echo "Unable to get file" 2>&1
exit 0
fi
Updated code using help:
if [ -f $BACKUP_DIR$1.mysoft.log ]; then
echo "Local log file exists, clean up for new copy..." 2>&1
/bin/rm $BACKUP_DIR$1.mysoft.log
exit 0
else
echo "File does not exist, getting a new copy..." 2>&1
echo "Checking remotely in $1 for foo logfile $REMOTE_MYCNF $STAMP" 2>&1
if [ ! -f $REMOTE_MYCNF ]; then
echo "File exists remotely, creating new logfile and bring a copy here...." 2>&1
ssh $1 "zcat $REMOTE_GZ >> $REMOTE_DIR$NEWLOG"
ssh $1 "cat $REMOTE_MYCNF >> $REMOTE_DIR$NEWLOG"
/usr/bin/scp $1:$REMOTE_DIR$NEWLOG $BACKUP_DIR$1.mysoft.log
echo "end remote copy" 2>&1
echo "Cleaning up remote files" 2>&1
ssh $1 "rm $REMOTE_DIR$NEWLOG"
exit 0
else
echo "Unable to get file" 2>&1
exit 0
fi
fi
The file test can be combined into one statement like this:
if [ -f $BACKUP_DIR$1.mysoft.log ]; then
At a glance, it doesn't look like you need to export any of the variables.
If you intend for the if [ ! -f $REMOTE_MYCNF ]; then block to be executed within the else of the previous if, just move it within it.
if ...
then
foo
else
if ...
then
bar
else
baz
fi
fi
If you need to check two things:
if [ "$foo" = "bar" && "$baz" = "qux" ]
Always quote your variables.
In a short script it's fine to use positional parameters such as $1 directly, but it makes a longer script easier to understand if a variables with meaningful names are assigned their values near the top.
remote_host=$1
When you want to echo errors to stderr do it this way:
echo "Message" >&2
The way you have it, you're echoing the message and any errors the echo itself may produce (pretty rare) to stdout.

Resources