• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need some help regarding command line arguments

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

For Now I have to pass 5 command line arguments.

sh test.sh 0 tomcat bin /app/tomcat /app/tomcat/bin/logs

I need to implement the disk usage part in a loop instead of writing multiple times that part.

diskusage_value1=`du -k $4 | tail -1 | awk '{ print $1}'`
echo $diskusage_value1
if [ $diskusage_value1 -ge $THRESHOLD_VALUE ]; then
echo "Reached the Threshold limit for \"$4 ($diskusage_value1%)\" on `hostname` as on `date`" |
mailx -s "Alert: Almost out of diskspace for $4" $MAILER_LIST
fi

diskusage_value2=`du -k $5 | tail -1 | awk '{ print $1}'`
if [ $diskusage_value2 -ge $THRESHOLD_VALUE ]; then
echo "Reached the Threshold limit for \"$5 ($diskusage_value2%)\" on `hostname` as on `date`" |
mailx -s "Alert: Almost out of diskspace for $5" $MAILER_LIST
fi

This part should be in loop instead of writing two times.

for that I tried this one not able to get the results.

i=4
n=`expr $# + 1`
a='$'

while test $i != $n
do
b=${a}$i
echo $b
diskusage_value1=`du -k $b | tail -1 | awk '{ print $1}'`
echo $diskusage_value1
if [ $diskusage_value1 -ge $THRESHOLD_VALUE ]; then
echo "Reached the Threshold limit for \"$4 ($diskusage_value1%)\" on `hostname` as on `date`" |
mailx -s "Alert: Almost out of diskspace for $4" $MAILER_LIST
fi
i=`expr $i + 1`
done
-----------------

diskusage_value1=`du -k $b | tail -1 | awk '{ print $1}'`

here $b becomes $4 not becoming $4value(/app/tomcat).
I think problem with this line b=${a}$i.
Please let me know how to solve this one.

Thanks,
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I quite understood that, but since what you're trying to do is a fairly common type of task, I'll offer a hint that may help.

Instead of an indexed loop you can use the "shift" function to lift items one by one from the command line until there are no more:

FILE = shift
du $FILE

I've not had to do it myself, so I'd have to RTFM to tell you how to test for no more items, but I've seen it done.

Another alternative is to simply iterate over the command list instead of consuming it:

for FILE in $@; do
du $FILE
done

Once again, I don't think this is quite everything, but it shouldn't be too hard to find some working examples.
reply
    Bookmark Topic Watch Topic
  • New Topic