Compatible Support Forums: Shell script for more than one command

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Shell script for more than one command

#1 User is offline   Dapper Dan 

  • Pooh-Bah
  • Group: Moderators
  • Posts: 1703
  • Joined: 27-September 03

Posted 02 December 2004 - 03:53 PM

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
0

#2 User is offline   CrazyKillerMan 

  • old hand
  • Group: Members
  • Posts: 761
  • Joined: 14-October 00

Posted 02 December 2004 - 05:29 PM

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?
0

#3 User is offline   Dapper Dan 

  • Pooh-Bah
  • Group: Moderators
  • Posts: 1703
  • Joined: 27-September 03

Posted 02 December 2004 - 06:04 PM

So do you need to have "**" between commands? Or can you just write commands one after another and they will run in that order?
0

#4 User is offline   CrazyKillerMan 

  • old hand
  • Group: Members
  • Posts: 761
  • Joined: 14-October 00

Posted 02 December 2004 - 06:22 PM

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.
0

#5 User is offline   CrazyKillerMan 

  • old hand
  • Group: Members
  • Posts: 761
  • Joined: 14-October 00

Posted 02 December 2004 - 06:29 PM

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


0

#6 User is offline   egorgry 

  • enthusiast
  • Group: Members
  • Posts: 315
  • Joined: 30-August 03

Posted 02 December 2004 - 09:46 PM

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.
0

#7 User is offline   Dapper Dan 

  • Pooh-Bah
  • Group: Moderators
  • Posts: 1703
  • Joined: 27-September 03

Posted 02 December 2004 - 10:13 PM

Sure! I think a thread of useful shell scripts everyone could share would be terrific!
0

#8 User is offline   blackpage 

  • member
  • Group: Members
  • Posts: 120
  • Joined: 23-March 04

Posted 03 December 2004 - 02:11 AM

Howdy DapperDan,

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

Code:
#!/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"


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:
NO-GO-EXAMPLE ...
cmdSeq="mkdir -p $baseDir ; cp /var/log/messages $outFile ; tail -n 5 $outFile"
$cmdSeq


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:
EXAMPLE: ensure proper exec. of previous cmd ...
cmdSeq="mkdir -p $baseDir & cp /var/log/messages $outFile & tail -n 5 $outFile"
$cmdSeq


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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users