LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to create bash (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-create-bash-4175451446/)

diskless.solve 02-23-2013 05:11 PM

How to create bash
 
Anyone can gimme some sample to make this bash file..
i need 4 Job in a single bash.

1.download file using lftp with lftp scripts
2.mount the disk to a folder (i did this and its works)
3.copy the file that being download to the mounted folder.
4.unmount the folder.

i did the no3. and its works...

the other keep coming out with an error that "theres no directories" o.O

here the sample
1.
#!/bin/bash
lftp -f /root/desktop/autoupdater/thescript.sh

3.
#!/bin/bash
cp /mnt/Temp /mnt/MyFolder

4.
#!/bin/bash
umount /mnt/MyFolder

anything wrong on this bash script... hope some one can help me out.. X.x

btmiller 02-23-2013 06:24 PM

Have you verified that the directories you are trying to access actually exist? For #2, which disk are you supposed to mount? You don't show us the mount command you use, so it's hard to tell if you're getting this right. Also, we need to see the contents of /root/desktop/autoupdate/thescript.sh to see what lftp is downloading and where it is putting them.

shivaa 02-23-2013 07:55 PM

Do you want something like by combining all these tasks:
Code:

#!/bin/bash
lftp -f /root/desktop/autoupdater/thescript.sh
mount -t <type>  <device>  <dir>
cp /mnt/Temp /mnt/MyFolder
umount /mnt/MyFolder

If you're stuck at some point, and in order to debug it step-by-step, just add a set -xv below shebang and invoke script again, as:
Code:

#!/bin/bash
set -xv
lftp -f /root/desktop/autoupdater/thescript.sh
mount -t <type>  <device>  <dir>
cp /mnt/Temp /mnt/MyFolder
umount /mnt/MyFolder

Also specify that what's your ultimate purpose of doing all these.

rigor 02-23-2013 08:01 PM

Hi diskless.solve !

First, usually in a Bash script/program, a line that starts with the "pound"/"hash" character, that is the # character, is a comment. It's only the first line of the file, where # followed by ! is special.

So if you want four commands in a single Bash program, you only need this line:

Code:

#!/bin/bash
as the first line; you don't need to repeat that line later in the file. Anywhere other than the first line, it's just a comment.

The -x option to Bash turns on "debugging" output, which displays program commands while the bash program is running. The +x options turns off debugging output.

You can see for yourself that #! on only the first line of the file, is special, by trying this Bash program. Put these lines in a single file, and execute the file:

Code:

#!/bin/bash -x

echo Hello.


#!/bin/bash +x

echo Goodbye.

If you put those lines in a file called:
Code:

test_bash.bash
then make the file "executable" by running this command in the directory where you put the file:

Code:

chmod  u+x  ./test_bash.bash
and finally execute the file this way:

Code:

./test_bash.bash

You'll see that both echo commands are displayed. If you change the -x on the first line to +x neither echo command will be displayed. That's because only the first line in the file is having any affect. The later line that starts with #! is just a comment, it has no affect in controlling the program. If it was having an effect controlling the program, it would turn off the debugging output, so you wouldn't see the second echo command displayed when the program runs.

So your Bash program should look more like this:

Code:

#!/bin/bash
lftp -f /root/desktop/autoupdater/thescript.sh

mount ...

cp /mnt/Temp /mnt/MyFolder

umount /mnt/MyFolder

where "mount ..." needs to be replaced by whatever the mount command actually is.

As has already been mentioned, we need to also have some idea what's in the lftp script, and you need to make sure that the directories you are trying to use, actually do exist.

If there's something private in the lftp script, or in the mount command such as usernames, passwords, whatever, just replace them with things like $USERNAME $PASSWORD etc., we'll understand what you mean.

We just need to make sure the structure of the lines in the lftp script file, and the structure of the mount command, are all correct.

Hope this helps!

diskless.solve 02-24-2013 03:28 AM

rigor.. i did try these bash

#!/bin/bash
lftp -f /root/desktop/autoupdater/thescript.sh

mount ...

cp /mnt/Temp /mnt/MyFolder

umount /mnt/MyFolder

but came out with the same error.. cant found the directories.. im using slackware... huh

grail 02-24-2013 05:04 AM

Again, is there a reason you do not show the complete mount command?

And as previously asked, what is the content of "thescript.sh"?

shivaa 02-24-2013 06:40 AM

If you're getting "no directories found" error, then better check all paths mentioned in your script, one-by-one.
Code:

~$ ls -la /path/to/file
And as already said above, use set -xv to debug the script.

diskless.solve 02-25-2013 02:16 PM

Ok let me explain... first this is the auto download game update for client server.. well actually its a Internet Cafe Server

here the sample for the script

1.
#!/bin/bash
lftp -f /root/desktop/autoupdater/thescript.lftp

2.
#!/bin/bash
mount -t ntfs-3g /dev/sdd1 /mnt/GAMES -o defaults,unmask=0

3.
#!/bin/bash
cp /mnt/Temp /mnt/MyFolder

4.
#!/bin/bash
umount /mnt/MyFolder

For the script no2 its work without any error...

no1 got an error even i use the set -xv
usr/bin/2.sh: line 1: !#: command not found
:No such file or directoryautoupdate/blabla.lftp

for no 3 and no 4, it came out with bad Interper

bash /usr/bin/3.sh: /bin/bash^M: bad interpreter: No such File or Directory

whats wrong on the script actually.. :-?

schneidz 02-25-2013 02:56 PM

the ^M's imply that you used windows to edit the script. windows uses some non-standard way to signal the end of a line which gets messy when transferred to another pc.

lleb 02-25-2013 06:18 PM

try this, should work for the most part.

Code:

#!/bin/bash

###########################################################
### Created by Ray Brunkow Feb. 25, 2013
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or version 3 of the
# license, at your option.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###
##########################################################

### use the -x option for troubleshooting.
### call the script with foo.sh -x

### setting variables
#############################

DOW=`date +%A
USER=user_for_ftp_site
PASS=password_for_ftp_site
PORT=port_number_if_not_standard_for_ftp_site
MOUNT=/path/to/device
FILE=file_name_to_be_downloaded
RDIR=/path/to/remote/directory/were/${FILE}/is/located
LOG=/path/to/log/file/${DOW}-log_name.log
STARTDATE="`date +%Y-%m-%d-%a-%H.%M.%S`"
ENDDATE="`date +%Y-%b-%d-%a-%H.%M.%S`"
BACKDIR=/path/to/mount/point
URL=ftp.foo.com

### Options
###################################

while getopts ":x" opt
do
        case $opt in
                x )        set -xvv
                        ;;
        esac
done

shift $(( $OPTIND -1 ))

### making lftp call
####################################

echo "Starting LFTP transfer of ${FILE} at ${STARTDATE}." >> ${LOG}

        lftp -e "set ftp:passive-mode on && cd ${RDIR} && get ${FILE} && bye" -u ${USER},${PASS} ${URL}

echo "End of LFTP transfer of ${FILE} at ${ENDDATE}" >> ${LOG}

### check if drive is already mounted
####################################

umount -f ${BACKDIR}
sleep 10

mount -t ext3 ${MOUNT} ${BACKDIR} >> ${LOG}
rc=$?
if [ $rc -ne 0 ]; then
  echo "[Backup] Unable to mount (rc=${rc}) can not copy file to destination.  Exiting script." >> ${LOG}
  exit 1
fi

### copy file to storage location
#####################################

echo "Starting rsync copy of ${FILE} to ${BACKDIR} at ${STARTDATE}." >> ${LOG}

rsync -aciHXv ${FILE} ${BACKDIR} >> ${LOG}

echo "End of rsync copy with Checksum comparison on ${FILE} at ${ENDDATE}." >> ${LOG}

### unmounting storage location
#####################################

umount -f ${BACKDIR}

exit

you will have to set the correct information for the variables as well as if you even need the port, username, password, etc... but this should function rather well for you. Its a modification of a lftp backup script I use to move an encrypted tarball to a NAS that has ftp enabled. works great for that, no reason it should not work great for you in reverse.

edit to add

from those of you who know MUCH more about both linux and bash scripting then I, please comment on my little script so I can learn as well.

Thank you all.

schneidz 02-25-2013 06:33 PM

^ the only thing i would have to critique would be use scp instead of ftp.

lleb 02-25-2013 06:38 PM

Quote:

Originally Posted by schneidz (Post 4899660)
^ the only thing i would have to critique would be use scp instead of ftp.

while i agree with the use of scp over ftp, the OP specifically asked for lftp script so i am guessing it might be a MS IIS FTP server he is pulling from.

If he is pulling a file from an other linux box id scrap 80% of my script and go pure rsync with ssh keys.

grail 02-25-2013 06:55 PM

So not to nitpick, but now that the mount has been included, apart from the editing in Windows, the obvious issue around the error being received would relate to:
Code:

# you are mounting
mount -t ntfs-3g /dev/sdd1 /mnt/GAMES -o defaults,unmask=0

# but you are copying to and unmounting
cp /mnt/Temp /mnt/MyFolder
umount /mnt/MyFolder

Clearly, "MyFolder" and "GAMES" are not the same location.

rigor 02-26-2013 04:27 AM

Please forgive this message...I thought I saw another issue with the script, but I finally realized it may have just been the control-M's. Unfortunately, there doesn't appear to be a way to completely delete my message...


All times are GMT -5. The time now is 12:56 AM.