0

I'm trying to create a script that will evaluate the output of a command line, and then print if it's larger than 200.

The program /exc/list will count the number of "stories" I have in a directory as an expression. For example:

/exc/list q show.today1.rundown

will return 161 if there are 161 stories in the today1 rundown.

I have to figure this for 23 different directories. If the number of stories is greater than 200, I need it to print it to a temp file (/tmp/StoryCount.$date).

What's the best method to handle this comparison?

  • Note, you didn't say what language you're using for your script. Is it bash? – NVRAM Aug 26 '13 at 18:13
  • Sorry, it is in bash. – Dan Jensen Aug 27 '13 at 21:14
  • NP, that's what I expected. – NVRAM Aug 27 '13 at 23:38
0

In bash, variables are essentially all strings (or arrays of strings). But the [ ] and [[ ]] command(s) have several operators that treat the arguments as integers: -lt -le -eq -ne -ge -gt

So, you could use:

for dir in ${LIST_OF_DIRECTORIES}; do
  if [[ $(/exc/list q ${dir}) -gt 200 ]]; then
    echo "${dir}"
  fi
done > /tmp/StoryCount.$(date +%y%m%d)

Note that if any directory name has whitespace, then this won't work as-is.

Also, the other answers use && rather than an explicit if for the conditional. They function the same for single statements and I tend to use && for my own scripts, but depending on who will be maintaining the script I tend to be more verbose/explicit.

| improve this answer | |
  • Actually, [[ supports using > instead of -gt. You only need -gt if you're using [ because > would be interpreted as a redirection. – Joseph R. Aug 26 '13 at 18:16
  • 1
    But that's a lexical sort - [[ 30 > 200 ]] returns true. – NVRAM Aug 26 '13 at 18:18
  • You're right. I was thinking of ((...)). – Joseph R. Aug 26 '13 at 18:22
  • Thanks for the great help in this! It worked wonderfully, though I'm now feeling a bit clunky. I can't get a list of directories to work for some reason, so I've once again ended up listing them all one by one: if [[ $(/exc/list q show.today1.rundown|wc -l) -gt 200 ]]; then echo "Caution: Today1 rundown has "$(/exc/list q show.today1.rundown|wc -l)" stories in it. Check it out." > /tmp/StoryCount.$date fi Any thoughts? – Dan Jensen Aug 30 '13 at 15:47
  • According to your OP the /exc/list q DIRNAME should print a number. Sorry, but I don't know that tool. My only advice is perhaps replace q with another flag? But I doubt this forum will be the best place to ask about that tool. – NVRAM Sep 4 '13 at 14:10
0

You can do this:

num=$(/exc/list q show.today1.rundown) #store command output in num
#sanitize num so the comparison doesn't break:
num=${num//\.[0-9]/} #remove numbers after a decimal point
num=${num//[^0-9]/} #remove any non-digit character
#if num is greater than 200 print it to a temporary file:
[ "$num" -gt 200 ] && printf "%d\n" "$num" > "/tmp/StoryCount.$(date)"

This of course assumes that num can never be negative; which is reasonably safe if you're using it to count something.

| improve this answer | |
0

You could store your 23 directories in an array in bash like so:

$ dirs=(show.today1.rundown show.today2.rundown)

-or-

$ dirs=(\
show.today1.rundown \
show.today2.rundown \
)

Then loop through them like so:

$ for dir in "${dirs[@]}"; do echo "$dir";done
show.today1.rundown
show.today2.rundown

You could then incorporate the /exc/list command inside the for loop like so:

dateStamp=$(date +"%F_%T")

for dir in "${dirs[@]}"; do

  cnt=$(/exc/list q $dir)  
  [ "$cnt" -gt 200 ] && printf "%d\n" "$cnt"

done | tee "/tmp/StoryCount.$dateStamp"
| improve this answer | |

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.