![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Oct 2004
Posts: 17
Rep Power: 0
![]() |
A simple script to execute a command package
Hi folks,
I'm prepared to write a simple script to execute 20+ commands and could not figure out how to make use of "loop" subject to certain conditions. If without conditions #!/bin/bash
cmd1="code1" #the exact/full command
cmd2="code2" #the exact/full command
cmd3="code3" #the exact/full command
etc.
cmd1 || exit 1
cmd2 || exit 2
cmd3 || exit 3
...
cmdn || exit n
echo "Build environment entered!"
exit 0It will be a long script. Please advise how to apply "loop" to shorter the script executing all command subject to following conditions 1) All commands except cmd3 should have no output. If there is any it needs to confirm either to continue OR to exit the shell 2) cmd3 should have "/mnt/lfs" output. If no output OR empty output the shell should exit, not continue executing further commands TIA satimis |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
Not really sure what you're going for here, but the number of lines required to initialize 20 variables and loop through them is greater than the number of lines required to simply list 20 commands... maybe I'm missing something
![]()
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
I'm guessing, from the wording ("full/exact command") that the idea is that the commands stored in the variables can be edited to adjust the behaviour of the script (kind of like setting CFLAGS etc. in a makefile).
I'm not a great bash guru, but it sounds like what you would really benefit from is a small subroutine (one that executes the command, checks for any output, and prompts as necessary). If you need information about functions, see http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html or the bash manpage. You could also use a loop to iterate over your values, but not if they're in simple scalar variables. Bash does support arrays, though (http://www.faqs.org/docs/bashman/bashref_71.html); you could loop over the array quite easily with a C-style for loop (http://www.tldp.org/LDP/abs/html/loops1.html introduces looping). I'm not exactly a bash expert myself, as I say, but hopefully that helps you. Last edited by mackenga; Aug 9th, 2005 at 2:19 PM. Reason: typo |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2004
Posts: 17
Rep Power: 0
![]() |
Hi folks,
Tks for your advice. Sorry for not posting more detail on my 1st message. I'm prepared to write a simple shell scripts executing following commands automatically. There are 25 commands to be executed in following sequence export LFS=/mnt/lfs
mount /dev/hda6 /mnt/lfs
mkdir -p $LFS
echo $LFS
/mnt/lfs (output)
/usr/sbin/chroot "$LFS" /tools/bin/env -i HOME=/root TERM="$TERM" PS1='\u:\w\$ ' PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin /tools/bin/bash --login +h
mknod -m 600 /dev/console c 5 1
mknod -m 666 /dev/null c 1 3
mount -n -t tmpfs none /dev
mknod -m 622 /dev/console c 5 1
mknod -m 666 /dev/null c 1 3
mknod -m 666 /dev/zero c 1 5
mknod -m 666 /dev/ptmx c 5 2
mknod -m 666 /dev/tty c 5 0
mknod -m 444 /dev/random c 1 8
mknod -m 444 /dev/urandom c 1 9
chown root:tty /dev/{console,ptmx,tty}
ln -s /proc/self/fd /dev/fd
ln -s /proc/self/fd/0 /dev/stdin
ln -s /proc/self/fd/1 /dev/stdout
ln -s /proc/self/fd/2 /dev/stderr
ln -s /proc/kcore /dev/core
mkdir /dev/pts
mkdir /dev/shm
mount -t devpts -o gid=4,mode=620 none /dev/pts
mount -t tmpfs none /dev/shmIn normal circumstance there is no output on all commands except command-4 with "/mnt/lfs" as output. If not considering the output it will be quite simple just listing all commands on a shell script. Now I expect to arrange the script as follows; 1) The script will automatically continue to execute command-5 if the output of command-4 is "/mnt/lfs" satisfying the condistion otherwise exit the shell. 2) If there is an output on any command excluding command-4 it needs to confirm "yes/no" with the corresponding command displayed on the terminal. 'yes" will continue on next command. "no" will exit the shell stopping the script. I'm considering whether shell script or perl script will do the job better. If shell script then how to start. If perl script then how to start. Which module/modules will be appropriate to apply, TIA B.R. satimis |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|