LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   three shell script questions (https://www.linuxquestions.org/questions/programming-9/three-shell-script-questions-419308/)

qrshat 02-24-2006 11:07 PM

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.

/bin/bash 02-25-2006 03:37 AM

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.

qrshat 02-25-2006 03:06 PM

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.

/bin/bash 02-26-2006 03:34 AM

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()

qrshat 02-27-2006 08:40 AM

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.


All times are GMT -5. The time now is 07:53 PM.