LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-25-2006, 12:07 AM   #1
qrshat
Member
 
Registered: Oct 2005
Posts: 56

Rep: Reputation: 15
three shell script questions


hi

here is three questions everybody will enjoy . and of course me.


1->i'm runnig a command in my shell script and i want to break this command if it doesn't return any return code or response in a certain time.

For example: i'm running tnsping command. if it does't return any response in 20 seconds, i want my script to echo an error mesage and to resumes its work.

how i can achive this contol in my script.

2-> i got a while block as follows,

cat $DBASEFILE | while read LINE
do
CONTROLDB=`echo $LINE| cut -d: -f1`
RESULT=`tnsping $CONTROLDB | grep 'OK' `
if [ $? -eq 0 ]
then
echo "$LINE ---> $RESULT"
continue
else
CONTROLIP=`echo $LINE| cut -d: -f2`
CONTROLSID=`echo $LINE| cut -d: -f4`
done

here DBASEFILE is a file store my database information. and i use tnsping to control databases connectivity.
and the question: i want to run 'tnsping $CONTROLDB' command at the background. while it is runnig it want to run the next 'tnsping $CONTROLDB'command with out concerned the result of the first 'tnsping $CONTROLDB'. ıs there a way to achive this control.

3->i got a log file: it context is like follow,

{sms} {90544} {90544258} {905358258} {7506}
{sms} {905438} {null} {1234} {9054329017} {chat} {NA}
{sms} {9054528}
{sms} {90543528} {null} {1234} {90593522} {chat} {NA}
{sms} {90571528} {null} {1234} {90546203} {chat} {NA}
{sms} {905471528} {null} {1234} {90546777} {chat} {NA}
{sms} {905471528} {null} {1234} {905466861} {chat}

i want do divede every line as follows part.
{sms}
{905438}
{90544258}
{905358258}
{7506}

i can do with some control loops. bu i am interested that is there a any simple way to perform this task. by the way, some times there are over the 1200 world in the {}. so awk isn't not a good choice for this task.


Thanks for your patience.
 
Old 02-25-2006, 04:37 AM   #2
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
3-> This is easy:
for i in $(<logfile);do echo $i;done

1-> This is a little harder:
Code:
#!/bin/bash
echo "Start..."
sleep 20 &
PROCESS=$!
count=0
while [ $PROCESS = "`pidof sleep`" ]
do
echo "Still running"
sleep 1
((count++))
echo $count
if [ $count = 19 ]
then
kill -9 "$PROCESS"
break
fi
done

echo "Done..."
The above example is using sleep but you just replace that with tnsping. The $! means the last process put in background. Try it and change the numbers and you'll see what it is doing.

2-> I'm not sure I understand this question. You want to run tnsping more than once in background?

<command1> & #command1 running in background
PROCESS1=$! #$PROCESS1 is the pid of command1

<command2> & #command2 running in background
PROCESS2=$! #$PROCESS2 is the pid of command2

wait #Don't do any more processing until all background processes complete.
 
Old 02-25-2006, 04:06 PM   #3
qrshat
Member
 
Registered: Oct 2005
Posts: 56

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by /bin/bash
3-> This is easy:
for i in $(<logfile);do echo $i;done
this not exactly what i want to do. becuse some times there more than one word in the {},maybe even thousend words.
for example:
{word1} {word1} {word1,word2,...wordn}



Quote:
Originally Posted by /bin/bash
1-> This is a little harder:
Code:
#!/bin/bash
echo "Start..."
sleep 20 &
PROCESS=$!
count=0
while [ $PROCESS = "`pidof sleep`" ]
do
echo "Still running"
sleep 1
((count++))
echo $count
if [ $count = 19 ]
then
kill -9 "$PROCESS"
break
fi
done

echo "Done..."
The above example is using sleep but you just replace that with tnsping. The $! means the last process put in background. Try it and change the numbers and you'll see what it is doing.
yes. it's exactly what i want to. Thanks.

Quote:
Originally Posted by /bin/bash

2-> I'm not sure I understand this question. You want to run tnsping more than once in background?

<command1> & #command1 running in background
PROCESS1=$! #$PROCESS1 is the pid of command1

<command2> & #command2 running in background
PROCESS2=$! #$PROCESS2 is the pid of command2

wait #Don't do any more processing until all background processes complete.

i want to run many commands at the background in a while LOOP and send the result of the these commands in the a log file. when one of these commands are running at the background and sending it result the log file, i want to CONTINUE the while LOOP for the next command that will run at the background.
 
Old 02-26-2006, 04:34 AM   #4
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
Quote:
this not exactly what i want to do. becuse some times there more than one word in the {},maybe even thousend words.
for example:
{word1} {word1} {word1,word2,...wordn}
It would still work even with your example. As long as there is no whitespace within the {} the way I did it should work. If you do have whitespace within the {} then this would be a better way:
sed 's/} /}\n/g' logfile


Quote:
i want to run many commands at the background in a while LOOP and send the result of the these commands in the a log file. when one of these commands are running at the background and sending it result the log file, i want to CONTINUE the while LOOP for the next command that will run at the background.
I don't see any reason you can't run the tnsping process many times in parallel in the background. As far as the results being printed to a log file you need to make sure you don't have the logfile open for writing by more than 1 process at a time. You can do that by using a lock file.
Code:
#!/bin/bash
MY_RESULTS="Bogus results"
until [ ! -f logfile.lock ]
do
sleep 1
echo "Waiting to write"
done
#Now I can write to logfile
touch logfile.lock
echo "$MY_RESULTS" >> logfile
rm logfile.lock
You would probably want to make the above a function that you can call, e.g. write_to_logfile()
 
Old 02-27-2006, 09:40 AM   #5
qrshat
Member
 
Registered: Oct 2005
Posts: 56

Original Poster
Rep: Reputation: 15
Thumbs up

Quote:
Originally Posted by /bin/bash
It would still work even with your example. As long as there is no whitespace within the {} the way I did it should work. If you do have whitespace within the {} then this would be a better way:
sed 's/} /}\n/g' logfile
sed is a better way for me. But \n doesn't works like what i expected. i am using solaris and Korn Shell for my script. I think, you're using bash for your script. is it a important point. When i using sed command with \n the result are as follows;
{sms}n{90571528}n{null}n{1234}n{90 54 6203}n{chat}n{NA}



Quote:
Originally Posted by /bin/bash
I don't see any reason you can't run the tnsping process many times in parallel in the background. As far as the results being printed to a log file you need to make sure you don't have the logfile open for writing by more than 1 process at a time. You can do that by using a lock file.
i want to run tnsping process parallel because the application on my systems is connected too many databases. and if one of the database is getting down, it is hard to examine which it is.

thaks for your help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
Couple of Shell Script Questions mathias1979 Programming 3 06-16-2005 04:22 PM
two shell script questions tutwabee Linux - General 4 08-10-2004 07:03 PM
Shell Script Questions Col Panic Linux - General 13 10-12-2003 01:51 PM


All times are GMT -5. The time now is 08:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration