Below is the code that was used when we used .netrc file for automatic login. But now we can't use auto login because of multiprotocol environment.So have to manually read the .netrc file and fetch username and password.This is generic download script will download files from server. I need some help in converting this script to read the file and fetch the username and password.
I have added the code i used when auto login was used. I need to now read the file and fetch username and password and use that in the script. Below is format of .netrc file machine ftp.test login test1 password test2 .I need to read ftp.test from my script and fetch test1(username) and test2(password) to do ftp.
. $HOME/env
. $LIB_PATH/miip_functions.shl
OPTIND=1;ftpop=;user=;hosts=;quote=
while getopts h:f:n:q: arg
do
case $arg in
h) hosts="$OPTARG"
;;
f) hosts=`cat $OPTARG`
;;
n) ftpop=-n
user="user $OPTARG"
;;
q) quote="$OPTARG"
;;
\?) logMessage ERROR "download.shl was used incorrectly."
endRun 1
;;
esac
done
shift `expr $OPTIND - 1`
if [ $# -ne 2 ] ; then
logMessage ERROR "download.shl was used incorrectly."
endRun 1
fi
dataset="'$1'"
filename=$2
file=`basename $2`
if [ -z "$hosts" ] ; then
hosts=`cat $LIB_PATH/ftp.hosts 2> /dev/null`
if [ -z "$hosts" ] ; then
hosts="ftp.test ftp.test2"
fi
fi
logMessage DLOAD "Starting FTP download of $file."
for host in $hosts
do
ftp -v $ftpop $host << ! > $TMPFILE.ftp 2>&1
$user
$quote
get $dataset $filename
!
egrep '^421 |^425 |^426 |^450 |^451 |^452 |^530 |^531 |^550 |^551
|^552|^553 |^590 |^Not connected' $TMPFILE.ftp > /dev/null 2>&1
rtn=$?
if [ $rtn -eq 1 ] ; then
break
fi
done
(echo ; echo -------------- ; echo $PROGNAME ; echo --------------) >> $RUNFILE
cat $TMPFILE.ftp >> $RUNFILE
rm -f $TMPFILE.ftp
if [ $rtn -eq 1 ] ; then
logMessage DLOAD "Completed FTP download of $file."
else
logMessage ERROR "Download of $file failed."
fi
`
Related
I'm trying to make a recycle bin for UNIX, so I have two scripts. 1 to delete the file and move it to the bin, the other script to restore the file back to its original location.
my restore script only works if the person gives the path to the deleted file.
ex: sh restore ~/trashbin/filename
How do I hardcode into my script so that I don't need to give the path to the deleted file it should already know to look in the trashbin for the file. My restore script works only when someone calls in the path to the file.
#!/bin/bash
rlink=$(readlink -e "$1")
rname=$(basename "$rlink")
function restoreFile() {
rlink=$(readlink -e "$1")
rname=$(basename "$rlink")
rorgpath=$(grep "$rname" ~/.restore.info | cut -d":" -f2)
rdirect=$(dirname "$rorgpath")
#echo $orgpath
if [ ! -d "$rdirect" ]
then
mkdir -p $rdirect
#echo $var
mv $rlink $rorgpath
else
mv $rlink $rorgpath
fi
}
if [ -z "$1" ]
then
echo "Error no filename provided."
exit 1
elif [ ! -f "$1" ]
then
echo "Error file does not exist."
exit 1
elif [ -f "$rorgpath" ]
then
echo "File already exists in original path."
read -p "Would you like to overwrite it? (y/n)" ovr
if [[ $ovr = y || $ovr = Y || $ovr = yes ]]
then
echo "Restoring File and overwriting."
restoreFile $1
grep -v "$rname" ~/.restore.info > ~/.restorebackup.info
mv ~/.restorebackup.info ~/.restore.info
fi
else
echo "Restoring file into original path."
restoreFile $1
grep -v "$rname" ~/.restore.info > ~/.restorebackup.info
mv ~/.restorebackup.info ~/.restore.info
fi
When you "remove" the file from the file-system to your trash-bin, move it so that its path is remembered. Example: removing file /home/user/file.txt should mean moving this file to ~/.trash/home/user/file.txt. That way, you'll be able to restore files to the original location, and you'll have auto-complete work, since you can do: sh restore ~/.trash/<TAB><TAB>
###Takes filenames as arguments and makes those executable
#create file variable
file=$*
chmod 755 $file
if [ $? -eq 0 ] ; then
echo permission change suceeded
else
echo permission change failed
exit 0
fi
This is my current code - I'm wanting to add an extra echo which will be "already got executable permission" - how would i add a check onto this to check that if it has executable permission or not
if [ -x "$file" ]; then
echo "already got executable permission"
else
....
fi
Check
help test
perm="$(stat -c "%a" $file)"
if [$perm -eq 755]; then
...
else
...
fi
I have written a script to transfer files from one server to another.
Firstly i am creating a tar of all the zipped files on one server and then i am transferring tar to another server.
Upon successful transfer i am doing Untar of files on both the sever.
I need to know one thing that how can i use Unix TRAP functionality in handling Restartability and Errors if anyhow tar file got corrupt of transfer got failed.
If any error exists i need to bring the execution back to its initial state.
Below mentioned is the small piece of code from my script
tar -cvf files_to_send.tar *.txt -C ${1}
RC=$?
if [ ${RC} -ne 0 ]; then
echo "Unable to Create Tar files of received files" >> ${LOG_FILE}
rm -f *_end >> ${LOG_FILE}
return 1
fi
cd ${1}
gzip files_to_send.tar
RC=$?
if [ ${RC} -ne 0 ]; then
echo "Unable to Create Zip of tar files" >> ${LOG_FILE}
echo "Deleting End Files" >> ${LOG_FILE}
rm -f *_end >> ${LOG_FILE}
echo "Deleting tar file" >> ${LOG_FILE}
rm -f ${1}/files_to_send.tar >> ${LOG_FILE}
return 1
fi
cd ${1}
chmod 775 files_to_send.tar.gz >> ${LOG_FILE}
RC=$?
if [ ${RC} -ne 0 ]; then
echo "Unable to change permissions of tar and end files" > ${LOG_FILE}
echo "Deleting End Files" >> ${LOG_FILE}
rm -f *_end >> ${LOG_FILE}
echo "Deleting Zipped tar file" >> ${LOG_FILE}
rm -f ${SOURCE_DIR}/files_to_send.tar.gz >> ${LOG_FILE}
return 1
You can do something like this :
trap 'do_something' ERR # start special error handling
tar ... # if the command fails, do_something will be executed
trap - ERR # stopping special error handling
do_something can be a function, a command or a script.
But you don't even need trap if you handle error cases properly. I wrote a version of your script with boolean logic, no need to store the return code in a variable, see :
if tar -cvf files_to_send.tar *.txt -C ${1}; then
echo "Unable to Create Tar files of received files" >> ${LOG_FILE}
rm -f *_end >> ${LOG_FILE}
return 1
fi
cd ${1}
if gzip files_to_send.tar; then
echo "Unable to Create Zip of tar files" >> ${LOG_FILE}
echo "Deleting End Files" >> ${LOG_FILE}
rm -f *_end >> ${LOG_FILE}
echo "Deleting tar file" >> ${LOG_FILE}
rm -f ${1}/files_to_send.tar >> ${LOG_FILE}
return 1
fi
cd ${1}
if chmod 775 files_to_send.tar.gz >> ${LOG_FILE}; then
echo "Unable to change permissions of tar and end files" > ${LOG_FILE}
echo "Deleting End Files" >> ${LOG_FILE}
rm -f *_end >> ${LOG_FILE}
echo "Deleting Zipped tar file" >> ${LOG_FILE}
rm -f ${SOURCE_DIR}/files_to_send.tar.gz >> ${LOG_FILE}
return 1
fi
All I want to do is move a file from my created recycle bin to it's own original path.
My code is as follows:
#!/bin/bash
restore ()
{
base=`basename $restore_file | cut -d"_" -f1`
echo "Your file $base has been restored"
mv deleted/$restore_file /$HOME/$base
}
restore_file=$1
if [ $# -eq 0 ]
then
echo "You have not entered a file"
elif [ -e deleted/$restore_file ]
then
restore
grep -v $(basename $restore_file) $HOME/.restore.info >> $HOME/.restore.inf o.tmp
cp $HOME/.restore.info.tmp $HOME/.restore.info
rm $HOME/.restore.info.tmp
else
echo "Your file "$restore_file" does not exist"
fi
I'm sure the error is in on line 7, just not sure how to rewrite. Any ideas?
Do you need to put a full path in before deleted?
You don't need the slash before $HOME, but that shouldn't be a problem.
How to write a shell script named "backup.sh" which accepts one parameter, which would be a filename/directory.
Create a backup copy of that with the .bak appended to its name.Show message on success.
If the file/directory does not exist, show a proper message.
i did up to this point.please help me to figure this out
#!/bin/sh
#create_backup.sh
And add a .bak
bak="${backup.sh}.bak"
if [ "$#" -eq 0 ]
then
exit 1;
echo "File Succesfully backuped"
fi
cp ${1} "${1}.back"
echo "File is not found "
exit 0
#!/bin/bash -e
directory=$1
cp -r $directory $directory.bak
echo "Success"
obvious caveats with pathing/error codes/etc