Shell script for more than one command

I'm sure the answer is out there, but I don't seem to be googling for the answer correctly and I keep coming up empty. How do you run more than one command in a shell script so that one command runs immediately after another is completed? Can ...




Topic Options
#151554 - 12/02/04 02:53 PM Shell script for more than one command
Dapper Dan Offline
Pooh-Bah

Registered: 09/27/03
Posts: 1703
Loc: The Sovereign state of South C...
I'm sure the answer is out there, but I don't seem to be googling for the answer correctly and I keep coming up empty. How do you run more than one command in a shell script so that one command runs immediately after another is completed? Can a string of commands be run in this way indefinitely?

Thanks

Top
Advertisement
#151559 - 12/02/04 04:29 PM Re: Shell script for more than one command
CrazyKillerMan Offline
old hand

Registered: 10/14/00
Posts: 761
I'm a little unsure what you mean. A script file will run from top to bottom and execute commands as it runs from the starting of line 1 to the end of line x.

Here is a snippet from some of my code from home:
Quote:

read $remote_dir
ftp -v -i $i <<**
put "./$file_extension.$date.$month.$year.tar.gz"
bye
**
rm ~/ftp_$file_extension.tar.gz
ncpumount ~/comptek


This will execute the rm command right after the ftp command...does this help?

Top
#151560 - 12/02/04 05:04 PM Re: Shell script for more than one command
Dapper Dan Offline
Pooh-Bah

Registered: 09/27/03
Posts: 1703
Loc: The Sovereign state of South C...
So do you need to have "**" between commands? Or can you just write commands one after another and they will run in that order?

Top
#151561 - 12/02/04 05:22 PM Re: Shell script for more than one command
CrazyKillerMan Offline
old hand

Registered: 10/14/00
Posts: 761
the ** is to give commands to the ftp program.

In a file, type in

Quote:
mkdir ./temp
touch ./temp/file.txt
echo "something" >> ./temp/file.txt


That should run in order...make sure to make it executable, though.

Top
#151563 - 12/02/04 05:29 PM Re: Shell script for more than one command
CrazyKillerMan Offline
old hand

Registered: 10/14/00
Posts: 761
Make sure to add the #!/bin/sh at the top as well.

This tells the PC what to use to truck on through it.

Quote:


#!/bin/sh
mkdir ./temp
touch ./temp/file.txt
echo "something" >> ./temp/file.txt


Top
#151577 - 12/02/04 08:46 PM Re: Shell script for more than one command
egorgry Offline
enthusiast

Registered: 08/30/03
Posts: 315
Loc: New Jersey
Crazykillerman has givng you teh answer. but...

What are you trying to acomplish I may already have a script you can hack at. When I get some time I'll post up some of my nautilus scripts I use for various things. Maybe some people can find them helpfull.

Top
#151582 - 12/02/04 09:13 PM Re: Shell script for more than one command
Dapper Dan Offline
Pooh-Bah

Registered: 09/27/03
Posts: 1703
Loc: The Sovereign state of South C...
Sure! I think a thread of useful shell scripts everyone could share would be terrific!

Top
#151602 - 12/03/04 01:11 AM Re: Shell script for more than one command
blackpage Offline
member

Registered: 03/23/04
Posts: 120
Howdy DapperDan,

you can concatenate multiple commands in a script e.g. in this way ...

Code:
[sup]#!/bin/sh
baseDir="~/script_commands"
outFile="$baseDir/testfile_$( date +%Y%m%d-%H%M%S ).txt"
alias cmdSeq="mkdir -p $baseDir ; cp /var/log/messages $outFile ; tail -n 5 $outFile"
cmdSeq
unset cmdSeq
echo "all done"[/sup]


The "alias"-thingie here is necessary and it is also often a quite useful method as e.g. a straight invocation of the commands would not work properly ...

Code:
[sup]NO-GO-EXAMPLE ...
cmdSeq="mkdir -p $baseDir ; cp /var/log/messages $outFile ; tail -n 5 $outFile"
$cmdSeq[/sup]


In the above example you'd get error-msgs about invalid commandline options for "mkdir".

A step further: If you want to ensure that the command-sequence only runs through if there are no errors encountered, you should concatenate the commands with ampersands ("&") like this ...

Code:
[sup]EXAMPLE: ensure proper exec. of previous cmd ...
cmdSeq="mkdir -p $baseDir & cp /var/log/messages $outFile & tail -n 5 $outFile"
$cmdSeq[/sup]


But I'm sure you already knew that one from compiling 2.4-kernels (make & make dep & make xyz & make world_go_round ...".

As it goes for a "script corner": I wholehartedly support this idea. And if anyone's interested, I could throw in e.g. an iptables-setup script that utilizes "arrays in bash-scripts" for hosts and services. Neat stuff regarding string handling and "loops" in there smile

hope that helps

Top


Forums
Windows Support Forums
Everything New Technology
Legacy OS
Hardware
Software
Games
Networking
Customization & Tweaking
Security

Linux Support Forums
Everything Linux
Linux Hardware
Linux Software
Linux Games
Linux Networking
Linux Customization & Tweaking
Linux Security

Apple Support Forums
Everything Apple
Recent Topics
MSDTC - Update causes Apps to fail
by Zman
6 minutes 38 seconds ago
Ntloader missing message when trying to install xp
by Complutenovice
07/10/09 04:20 AM
Odd 3DO malfuction.
by one thread wonder
07/09/09 06:53 AM
DosBox v0.73 Released
by jmmijo
07/09/09 04:20 AM
qtech winxp pc
by johnmony
07/08/09 12:58 AM
Who's Online
1 Registered (Zman), 165 Guests and 13 Spiders online.
Key: Admin, Global Mod, Mod
Forum Stats
90558 Members
24 Forums
52396 Topics
182627 Posts

Max Online: 1079 @ 03/12/08 01:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22