This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes need some help regarding command line arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "need some help regarding command line arguments" Watch "need some help regarding command line arguments" New topic
Author

need some help regarding command line arguments

Chethan Sharma
Ranch Hand

Joined: Mar 22, 2006
Posts: 59
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,


chaithu
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need some help regarding command line arguments
 
Similar Threads
Tomcat fails to restart with "Address already in use" exception
Search on a string
Java and Shell scripts
Out;put is not getting in this Shell Script??
error starting up 2nd instance of tomcat