Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store a result of a command into a variable using csh

Tags:

unix

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.

like image 301
thicksauce Avatar asked Sep 01 '25 16:09

thicksauce


1 Answers

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`
like image 186
Elliott Frisch Avatar answered Sep 05 '25 13:09

Elliott Frisch