LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   need some help regarding command line arguments (https://www.linuxquestions.org/questions/linux-general-1/need-some-help-regarding-command-line-arguments-521997/)

kristam269 01-23-2007 07:08 AM

need some help regarding command line arguments
 
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,

theNbomr 01-23-2007 09:40 AM

It seems you should try using the bash builtin variable '$#', which contains the number of arguments. This can be used as your loop counter, which is what it appears you are trying to achieve. Also, you probably want to apply the shift operator, to shift each argument into '$1'. This snippet (untested) should demonstrate.
Code:

#! /bin/sh

echo $#;
i=0
j=$#

while [ $i -lt $j ] ; do
echo $i $1
shift
i=`expr $i + 1`;
done

--- rod.


All times are GMT -5. The time now is 06:31 PM.