0

I created a shell script which I am running using nohup. This script runs various sql script in sequence but few in parallel also. I have the following statements in my script-

echo exit | sqlplus -s ${username}/${pwd}@${DB} @close1.sql
echo exit | sqlplus -s ${username}/${pwd}@${DB} @close2.sql

echo exit | sqlplus -s ${username}/${pwd}@${DB} @insertPricing1.sql &
pid1=$!
echo exit | sqlplus -s ${username}/${pwd}@${DB} @insertPricing2.sql &
pid2=$!

echo "Pricing Insert PIDs => ${pid1}, ${pid2}"

while [ `ps -p ${pid1},${pid2} | wc -l` > 1 ]
do
sleep 5
done

echo exit | sqlplus -s ${username}/${pwd}@${DB} @insertPricing3.sql

The intention is to run close1 -> close2 -> insertPricing1 & insertingPricing2 in parallel -> insertPricing3. where -> means in sequence.

When I checked the result the next day (after sufficient time it should have been completed), I saw that the shell script was still running. Pricing1 and Pricing2 were done but Pricing3 didn't start. The processes for 1 and 2 had finished.

ps -p 19105,19107

  PID TTY          TIME CMD

There is some problem in the while loop as when I run this in

 # ps -p 19105,19107 | wc -l
1

but this-

# while [ `ps -p 19105,19107 | wc -l` > 1 ]
> do
> echo "hello"
> done
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
........ ctrl+C

so why this loop works when 1 is not greater than 1? What should be the solution?

2

Thanks to @steeldriver's comment for helping me out. It was a silly mistake from my side. > is considered as redirection operator inside [ ] (or most of the places in a shell script). The standard way to use is -gt

For comparing integers as per the answer in the link-

-eq  #Is equal
-ne  #Is not equal
-lt  #Less than
-le  #Less than or equal
-gt  #Greater than
-ge  #Greater than or equal
  • 1
    You can use > for comparison, but for string comparison, and it has to be escaped like \>. – Tomasz Mar 26 '17 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.