write a shell script to execute a command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting write a shell script to execute a command
# 1  
Old 06-28-2009
write a shell script to execute a command

Hello all,
I have just started doing shell scripting.
I want to read a file which stores the status of my job I have submitted
on a cluster.
The file looks something like this :
========================FILE=============================

crab: Checking the status of all jobs: please wait
crab:
ID STATUS E_HOST EXE_EXIT_CODE JOB_EXIT_STATUS
--------------------------------------------------------------------------------------------
1 Done
2 Done

crab: 2 Total Jobs
=====================================================

The highlighted lines display the status of the jobs. The status shown is done. Whenever it is done I want to give a command which is "getoutput" to retrieve my jobs.
So I want to write a small script which can read this file and whenever it is
done it just retrieves the output.
Could somebody tell me if there is a way to do this using shell script?

This is something which I tried doing but didnt work. "out" is file which stores the output info

==================================line========================================

flag[1]=0
flag[2]=0
cat out | gawk 'BEGIN{i=1;}{if($2=="Done") { $flag[i]=1; }'

if($flag[1]==1 && $flag[2]==1); then
getoutput
fi
================================================================================

But this doesnt seem to work because it seems that its treating flag[1] & flag[2] as local variables inside
gawk.


Thanks
learn_linux

Last edited by learn_linux; 06-28-2009 at 03:54 PM..
# 2  
Old 06-28-2009
Quote:
Originally Posted by learn_linux
...
So I want to write a small script which can read this file and whenever it is done it just retrieves the output.
...
Code:
$ 
$ cat file1
crab: Checking the status of all jobs: please wait
crab:
ID STATUS E_HOST EXE_EXIT_CODE JOB_EXIT_STATUS
--------------------------------------------------------------------------------------------
1 Done
2 Done

crab: 2 Total Jobs
$ 
$ awk '$2=="Done"' file1
1 Done
2 Done
$

tyler_durden
# 3  
Old 06-29-2009
Hi,
Thanks for the reply. I do not want to print "Done".
I am running a job on a cluster. The output of which remains stored there until I say "getoutput" on my terminal which then brings the output on my hard disk. What I do is I first check the status of the job stored in a file "out" (in my case) and then if the job is done I say "getoutput".
I want to write a shell script which can execute a command "getoutput" (this is a command which is specific to a cluster I am working on) after checking if the job status is "Done".

I have written a line which is trying to do the same:

========================CODE====================
flag[1]=0
flag[2]=0
cat out | gawk 'BEGIN{i=1;}{if($2=="Done") { $flag[i]=1; i=i+1;}'

if($flag[1]==1 && $flag[2]==1); then
getoutput
fi

======================================================

But the problem here is that after the gawk is over, the values of flags are still 0 even if $2=="Done" i.e. gawk is not changing the value of flags globally (only locally) which should get changed if $2=="Done"

Thanks,
learn_linux
# 4  
Old 06-29-2009
Is it a coincidence that you have declared two elements of that array when "Done" occurs twice in the "out" file ?

If "Done" had occurred thrice, would you have declared 3 array elements ? i.e. do you need to know the number of times "Done" occurs before you can declare the array elements ?

Or do you not care if "Done" occurs more than twice ? Which means you execute "getoutput" when "Done" occurs at least twice. (or is it exactly twice.)

tyler_durden
# 5  
Old 06-29-2009
Hi,
I want to execute "getoutput" when "Done" appears exactly twice.

I think my problem would be solved if I know how to change the value of a shell
variable inside gawk.

This is something I tried :
============================================================
flag[1]=0
flag[2]=0
cat out | gawk 'BEGIN{i=1; print "flag inside begin = "; print /'$flag[1]'/;} { if($2=="Done") { /{$flag[i]=1}/; i=i+1; print "flag[i] inside if = "; print /'$flag[i]'/; }} '

echo "flag after awk = "
echo {$flag[1]}
============================================================


And here is the output:
============================output=============================
flag inside begin = 0
flag[i] inside if = 0
flag[i] inside if = 0
flag after awk = 0
=============================================================

Though I have tried changing the value of flag inside gawk by doing :
/{$flag[i]=1}/

It seems that inside awk, there is someother way to change the vaue of a shell variable.

Thanks,
learn_linux
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute application command in shell script?

friends, i have application commands to execute in shell script it is something like this. i will login with user data it takes me to $ prompt. Example given below login: data ps : xxxx $ MNSinput --this takes me to greater than prompt > > ops --this is command i enter this output... (7 Replies)
Discussion started by: udaykrishna
7 Replies

2. Shell Programming and Scripting

Need to execute Oracle relocation command in shell script

Hello, I need to execute below command in shell script srvctl relocate service -d $database -s $service -i $avail -t $pref -f and also need to get the errors ,if any,in another file. What's the right way to execute such commands in shell script? Best regards, Vishal (1 Reply)
Discussion started by: Vishal_dba
1 Replies

3. Shell Programming and Scripting

When i am trying to execute export command within a shell script it is saying command not found.

I am running the export command within a view to use that value inside my build script. But while executing it it is saying "export command not found" My code is as follows: -------------------------- #!/bin/sh user="test" DIR="/bldtmp/"$user VIEW="test.view1" echo "TMPDIR before export... (4 Replies)
Discussion started by: dchoudhury
4 Replies

4. UNIX for Dummies Questions & Answers

How to execute command after telneting in shell script?

Hi , I have to write a shell script to telnet to specific host and execute the admin command there. Please help me to do that. Eg : telnet hostname portno admin command exit (3 Replies)
Discussion started by: arukuku
3 Replies

5. UNIX for Dummies Questions & Answers

ssh command to execute shell script in another server

ssh -q <hostname> /opt/tcs/satish/tst.ksh ssh -q <anotherserver> /opt/tcs/satish/tst.ksh tst.ksh has "nohup <command> & " when i execute below script , its throwing error as nohup can not be found ssh -q <anotherserver> /opt/tcs/satish/tst.ksh > log & can someone let me... (5 Replies)
Discussion started by: only4satish
5 Replies

6. Shell Programming and Scripting

Execute a shell script after a particular command is run

Hi, I need to run a script whenever the Cron file is modified. The requirement is whenever a user modifies the cron file, the script should run automatically. Can you please provide your inputs ? (5 Replies)
Discussion started by: harneet2004us
5 Replies

7. Shell Programming and Scripting

Need to Write Shell Script based off of this shell command

I'm trying to read a bunch of log files and output the lines that contain particular strings. To accomplish this, I've been running the following from the command line: find . -name "*" | xargs grep " " | grep " " > output.txt Two grep statements are needed in case I'm looking for a... (3 Replies)
Discussion started by: Rally_Point
3 Replies

8. Shell Programming and Scripting

shell script to execute user command

I don't know why the following shell script doesn't work. Could you please help me out? #!/usr/bin/ksh test="cal > /tmp/tmp.txt 2>&1" $test I know it will work for the following format: #!/usr/bin/ksh cal > /tmp/tmp.txt 2>&1 However, I need to get the command from the user in... (1 Reply)
Discussion started by: redtiger
1 Replies

9. UNIX for Dummies Questions & Answers

Plz Help : How to use write command to execute command on some other terminal

Hi Group , I m trying to execute commands on some other system using write command but inspite of executing the commands they r passed as simple messages. - i m writing >write user-id ! ls o ctrl-d inspite of executing the command ls,other terminal shows ! ls. Thnx in advance. (2 Replies)
Discussion started by: Aashish
2 Replies

10. UNIX for Dummies Questions & Answers

Urgent!! How to write a shell program to execute command to access internet?

hi, I am new ot unix. So, can i write a shell(c shell or korn shell) program to access internet? I mean if I run the program, it can access specified url and then copy the html to a file? Can anyone help me? And how can make the program runs every 1 hr? new comer (2 Replies)
Discussion started by: firebirdonfire
2 Replies
Login or Register to Ask a Question