thicksauce
thicksauce

Reputation: 65

store a result of a command into a variable using csh

I'm trying to store a result of a command into a variable so I can display it nicely along with some text in one long not have it display the output and then newline then my text, in my csh script.

#! /bin/csh -f


if ("$1" == "-f" && $#argv == 1 ) then
    grep 'su root' /var/adm/messages.[0-9] | cut -c 21-250
    grep 'su root' /var/adm/messages
else if( $#argv >  0 ) then
    echo "Usage : [-f]"
else
  grep 'su root' /var/adm/messages.[0-9] /var/adm/messages | wc -l
  printf "failed su attempts between Nov 02 and Oct 28\n"
endif

this command in the script

  grep 'su root' /var/adm/messages.[0-9] /var/adm/messages | wc -l

gives me 21 when i run it, and i want 21 to be stored in a variable.

so i can just display the output of

21 failed su attempts between Nov 02 and Oct 28

and not

21
failed su attempts between Nov 02 and Oct 28

or if theres an easier way that doesn't involve variables open to that too.

Upvotes: 3

Views: 6971

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201439

You can use set and backticks (``). Something like

set count=`grep 'su root' /var/adm/messages.[0-9] /var/adm/messages | wc -l`
printf "$count failed su attempts between Nov 02 and Oct 28\n"

or

printf "%s failed su attempts between Nov 02 and Oct 28\n" "$count"

or without a variable, like

printf "%s failed su attempts between Nov 02 and Oct 28\n" \
    `grep 'su root' /var/adm/messages.[0-9] /var/adm/messages | wc -l`

Upvotes: 2

Related Questions