I have created this script to keep checking disk usage in a direcorty every 2 seconds with command du -sh
So when I run the script with ./repeat.sh du -sh it outputs:
52K .
52K .
52K .
etc ...
Here is the script:
#!/bin/sh
while true
do
if cmd="$1" "$@"
then sleep 2
fi
done
I am trying to alter it so that if no arguments are provided e.g. the sh bit or the command provided is incorrect it exits without doing anything but this bit is not working in the script. How do I set up these conditions to what I have ?
Thank you
You can test the validity of the command by first running it silently and validating its return code (since you’re about to run it in a loop anyway, you sacrifice one execution to verify it):
As for checking arguments, this is a simple check of the
$2argument:Combining these features should get you what you want in your script.