Raspimina
Posts: 58
Joined: Thu Feb 27, 2014 4:51 pm

managing run script from onother script

Tue Mar 11, 2014 3:35 pm

Hi guys, it wuld be nice if i can separate 2 lines of the script and call it from external file.

this is the original script:

Code: Select all

sudo mount -t vfat -o uid=pi,gid=pi /dev/sda1 /data
cd /data/LxT_Data/ && sudo zip -rT -0 /ftp/`ls | egrep '[0-9]{8}.LD0.s'`.zip ./`ls | egrep '[0-9]{8}.LD0.s'`

cd /ftp && find * -type f -exec curl -u ftpuser:ftppass --ftp-create-dirs -T {} ftp://website.org/data/{} \;

rm -r /data/* -f -v
rm -r /ftp/* -f -v

cd /log && sendemail -f [email protected] -t [email protected] -u "Mail Object" -s mail.smtp -o tls=yes -xu [email protected] -xp smtppassword -m "text"

sleep 300 && sudo halt

i think the correct way is this:
but what is the correct command to lounch the external files?
i want that the execution order must remain the same as the original
master script wrote:sudo mount -t vfat -o uid=pi,gid=pi /dev/sda1 /data
cd /data/LxT_Data/ && sudo zip -rT -0 /ftp/`ls | egrep '[0-9]{8}.LD0.s'`.zip ./`ls | egrep '[0-9]{8}.LD0.s'`

(bash command to run script) /ftp-command

rm -r /data/* -f -v
rm -r /ftp/* -f -v

(bash command to run script) /mail-command

sleep 300 && sudo halt
ftp-command wrote:cd /ftp && find * -type f -exec curl -u ftpuser:ftppass --ftp-create-dirs -T {} ftp://website.org/data/{} \;
mail-command wrote:cd /log && sendemail -f [email protected] -t [email protected] -u "Mail Object" -s mail.smtp -o tls=yes -xu [email protected] -xp smtppassword -m "text"

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: managing run script from onother script

Tue Mar 11, 2014 3:43 pm

The simple way is to use the "." (dot) command, like this:

# main script
# do stuff
. otherscript
# do more stuff
. anotherscript
# etc

The trick is working out the PATHs and whether you need to be able to run it from anywhere.

That is left as an exercise for the reader…

Note to others: I used the "." command instead of just "running" it, because newbies always get frustrated by variable-scoping issues if they go the "running it" route.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Raspimina
Posts: 58
Joined: Thu Feb 27, 2014 4:51 pm

Re: managing run script from onother script

Tue Mar 11, 2014 4:12 pm

thanks, i will try it! :)
the execution order ramain the same as the original script right?


so the master script will be something like this:

Code: Select all

sudo mount -t vfat -o uid=pi,gid=pi /dev/sda1 /data
cd /data/LxT_Data/ && sudo zip -rT -0 /ftp/`ls | egrep '[0-9]{8}.LD0.s'`.zip ./`ls | egrep '[0-9]{8}.LD0.s'`

.ftp-command

rm -r /data/* -f -v
rm -r /ftp/* -f -v

.mail-command

sleep 300 && sudo halt

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: managing run script from onother script

Thu Mar 13, 2014 7:03 pm

Right.

And, just to be clear, the "dot" command "sources" the other script, so the effect is as if the other script were textually included into the main script at that point. Much like '#include …' in C (and other such languages).
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)


wimble
Posts: 34
Joined: Tue Feb 05, 2013 9:52 am

Re: managing run script from onother script

Thu Mar 13, 2014 7:33 pm

That needs to be "dot space script", not "dot script".

So

Code: Select all

. ftp-command
Also "ftp-command" must either be in the current directory, or in one of the directories in $PATH, or you have to give the full pathname. Since you're changing directory within the script, the first option isn't going to work. Personally, I'd go for the third, which means you put all three scripts in a single directory, and create a variable with that directory name, and then use that to invoke the subsidiary scripts:

Code: Select all

scriptdir=/home/pi
sudo mount -t vfat -o uid=pi,gid=pi /dev/sda1 /data
cd /data/LxT_Data/ && sudo zip -rT -0 /ftp/`ls | egrep '[0-9]{8}.LD0.s'`.zip ./`ls | egrep '[0-9]{8}.LD0.s'`

. $scriptdir/ftp-command

rm -r /data/* -f -v
rm -r /ftp/* -f -v

. $scriptdir/mail-command

sleep 300 && sudo halt
changing scriptdir as appropriate. Doing it this way avoids the possibility of you accidentally running some other "ftp-command" script, if there happens to be one on your system (there almost certainly isn't, but better safe than sorry!)

Return to “General discussion”