0

I have a shell script that is suppose to take the output of autorep command and use that output in the IF statement but its not working

#!/bin/bash

 read -r i

output=$(autorep -j $i -q | awk '/^insert_job:/ {printf "%s\n", $4}')

if [ $output == "CMD" ] || [ $output == "BOX" ];
  then
 echo "its a command or box job"

else

echo "Not a command or box job"

fi

if i modify this script to echo the output of the command

#!/bin/bash

 read -r i

output=$(autorep -j $i -q | awk '/^insert_job:/ {printf "%s\n", $4}')

echo $output

then I get the result as:

CMD CMD CMD BOX BOX BOX CMD BOX

but the output should come on each line

The $i is suppose to be a Wildcard that resolves to a series of jobs. Is there anything I am doing wrong here?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
NecroCoder
  • 15
  • 4
  • What input do you call your script with? – joanis Dec 23 '22 at 15:23
  • 1
    And is this `autorep` from Autosys Workload Automation? If so, you should add the tag `autosys` to your question so people who know that system might see it. But to help non-Autosys people help you, can you show the output of `autorep -j $i -q` for a value if `$i` you might provide? – joanis Dec 23 '22 at 15:26
  • 1
    BTW, if you run your code through http://shellcheck.net/ it'll point out some general-purpose robustness improvements. For example, `[ $output == "CMD" ]` should instead be `[ "$output" = CMD ]`: you don't need to quote a fixed constant string that always parses to itself, but you _do_ need to quote parameter expansions to prevent word-splitting and globbing; and `[` is the `test` command, which has only one [documented/standardized](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html) string comparison operator, and that operator is `=`. – Charles Duffy Dec 23 '22 at 15:28
  • Also, `echo "$output"`; that lack of quotes, for that matter, is the complete cause of output _not_ being line-oriented, for reasons given in a different question already in our knowledge base: [I just assigned a variable, but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Dec 23 '22 at 15:29
  • BTW, think about `case $output in *CMD*|*BOX*) echo "It's a command or box job";; *) echo "Not a command or box job";; esac` – Charles Duffy Dec 23 '22 at 15:31
  • Note the asterisks above: If your `autorep | awk` command has multiple lines of output, then `[ "$output" = CMD ]` will only be false even if one of those lines is `CMD`, because it's comparing the _entire string_ against `CMD` – Charles Duffy Dec 23 '22 at 15:33
  • And to stop your wildcard from being expanded by the shell before it gets to `autorep`, you want quotes there too: `autorep -j "$i"` – Charles Duffy Dec 23 '22 at 15:33

0 Answers0