This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need some help regarding command line arguments

 
Chethan Sharma
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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,
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic