0

I have written a script to find out the current processes running on the system with their project name.But after executing script i m getting too many line with status code = 0 as output.Can anyone please help me on this i am new to scripting.

 #!/bin/bash
 dsjob -lprojects >ProjectName.txt   #Fetching the datastage project name running on the             server                  

 ps -ef | grep DSD.RUN | cut -d" " -f21 > Currentjoblog.txt  #this will contains the     current running job on the server

 for i in $(< ProjectName.txt);do
 dsjob -ljobs $i > $i.txt 
 for j in $(< $i.txt);do
 cat $Currentjoblog.txt | while read LINE
 do
 if [ x$j == x$LINE ] ;then
  echo "$i-------$LINE"
 fi
 done <"$CurrentJoblog.txt"
 done
 done
Baba
  • 852
  • 1
  • 17
  • 31
user3206597
  • 23
  • 1
  • 3
  • Testing strings using the idiom `x$var = xstring` is only required in very old shells which will very likely not recognize the validity of `==` (they generally run on systems on which the `test` (aka `[`) command requires `=`). – William Pursell Jan 17 '14 at 17:39

2 Answers2

2

First, you don't need to use temporary files :

dsjob -lprojects|while read l1; do
     dsjob -ljobs $l1|while read l2; do
         # you could use $l2 here
         # ...
     done
done

Here is a reminder on how to read a command output or file content with a loop in Bash : here

Otherwise :

cat $Currentjoblog.txt | while read LINE
do
    # ...
done <"$CurrentJoblog.txt"

This syntax is wrong. Either you read the output of the cat, or you read the file content by redirection but not both (see my previous link for the right syntax).

Otherwise :

if [ x$j == x$LINE ] ;then
    # ...
fi

You have to protect your variables with doubles quotes, when you use the command test or [ syntaxe instead of [[. I also think that the x which foregoing the operands are errors ;)

Here is a reminder about the necessity (or not) to protect your variables with double quotes : here.

Community
  • 1
  • 1
Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
0

Maybe you want to use pgrep which "looks up or signals processes based on name and other"

example:

$> pgrep $(dsjob -lprojects)
Quirysse
  • 71
  • 6