Quote:
Originally Posted by hamimi
Hi ,
I have a script:
#!/bin/bash
echo enter new tag:
read NEWTAG
awk "{\$7=\"$NEWTAG\"; print \$0}" myfile > newfile
echo "Running Restore on 10.3.13.25"
sh newfile;monrst
I needs to run "newfile" & "monrst" together so that if newfile is complete it e-mails a message.
NB: my script monrst already has the e-mail script inside to check for process.
THanks
Hamim
|
I don't understand the purpose of this all.
That script will read $SOMETHING, change the 7th field on a given file by that $SOMETHING and write it to a new file. The it will call other two different scripts.
If you want to run the "monrst" *after* -and only IF- "newfile" completes, you can just concatenate it with the logical AND operator, which is the double ampersand '&&'. This way:
Code:
#!/bin/bash
echo enter new tag:
read NEWTAG
awk "{\$7=\"$NEWTAG\"; print \$0}" myfile > newfile
echo "Running Restore on 10.3.13.25"
sh newfile && sh monrst
That will ensure that monrst is launched *after* newfile
and only if newfile was successful.
Quote:
Originally Posted by blackhole54
You want to run one or both scripts "in the background."
|
I don't think he wants to run anything on the background, but since I only understand the OP partially, I can't be sure. In any case, if he wants to ensure synchronization, he must use some bash magic:
Code:
#!/bin/bash
echo enter new tag:
read NEWTAG
awk "{\$7=\"$NEWTAG\"; print \$0}" myfile > newfile
echo "Running Restore on 10.3.13.25"
# saves the return state
ret=$(sh newfile &)
# captures the PID
pid=$!
# do whatever you want in the middle
# while sh newfile is run in the background
# waits for sh newfile to finish
wait $pid
# and then continue with monrst, if ret is ok
test -z $ret && sh monrst
Completely untested, of course, but I think it should work. $! is bash specific though, so, if you are really using the legacy sh, you are going to need another way to get the pid of the application so you can wait for it to finish.
Quote:
First, if your scripts start with the line #!/bin/sh or #!/bin/bash and have their executable bit set, then you can run them w/o specifically calling sh or bash.
|
Which is not a good thing to assume if you are going to use this script on many locations, since UIDS are going to be different, and you don't want to go chmoding scripts all the way. Anyway, adding the sh in front of it doesn't hurt in any way and you have one less thing to worry about when scripting.
Quote:
Originally Posted by chrism01
I believe that's double ampersand:
monrst && newfile
|
Since I don't fully understand this, I don't really know what do the OP mean. The difference between the two is simple: && is a logical AND operator, and & is a mechanism to put the actual command into the background as long as it's launched (so, other commands can continue running concurrently).
A logical AND is evaluated sequentially, and if any of the members is not true (in bash, that means "non zero"), then the whole sentence is false (and hence, no additional evaluation is needed).
So, if you do "command1 && command2 && command3" and command1 fails, the return state of command1 will be non-zero, and no further commands will be executed (because there's no need to evaluate anything more, because the whole sentence will be false anyway.
On the contrary, the OR operator will continue to run commands until one of them ends ok (zero), which I sometimes use to build quick sentences like this:
Code:
test -f /foo && echo "/foo exists" || echo "/foo doesn't exist"
Which would be some kind of quick if...else construct.