LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2013, 05:11 PM   #1
diskless.solve
LQ Newbie
 
Registered: Feb 2013
Posts: 11

Rep: Reputation: Disabled
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
 
Old 02-23-2013, 06:24 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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.
 
Old 02-23-2013, 07:55 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.
 
Old 02-23-2013, 08:01 PM   #4
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
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!
 
Old 02-24-2013, 03:28 AM   #5
diskless.solve
LQ Newbie
 
Registered: Feb 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-24-2013, 05:04 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Again, is there a reason you do not show the complete mount command?

And as previously asked, what is the content of "thescript.sh"?
 
Old 02-24-2013, 06:40 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.
 
Old 02-25-2013, 02:16 PM   #8
diskless.solve
LQ Newbie
 
Registered: Feb 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.. :-?
 
Old 02-25-2013, 02:56 PM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.
 
Old 02-25-2013, 06:18 PM   #10
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.

Last edited by lleb; 02-25-2013 at 08:26 PM.
 
1 members found this post helpful.
Old 02-25-2013, 06:33 PM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
^ the only thing i would have to critique would be use scp instead of ftp.
 
Old 02-25-2013, 06:38 PM   #12
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by schneidz View Post
^ 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.
 
Old 02-25-2013, 06:55 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
 
Old 02-26-2013, 04:27 AM   #14
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
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...

Last edited by rigor; 02-26-2013 at 04:34 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is it possible to create a bash script to create multiple new bash scripts Batistuta_g_2000 Linux - Newbie 6 02-19-2013 11:42 AM
how to create this on bash packets Programming 7 01-09-2012 07:36 PM
Help me to create a bash script Mr. Alex Linux - Newbie 5 06-14-2011 12:51 AM
[SOLVED] How do you create a BASH shortcut? lupusarcanus Linux - Newbie 26 02-25-2010 05:04 PM
Create a bash file trolojik1 Programming 6 05-26-2008 02:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration