I'm writing a shell script that can take zero to 2 user arguments.

I have an if statement to check to see if any arguments were entered and then I have a case to enter the arguments into the correct variables. This all works great.

Now, I want to add checking to see if someone adds the argument without a parameter, and then return an appropriate message.

For example, this is the script usage:

psmonitor [-t tseconds] [-n count]

I want checking to so that if someone enters:

psmonitor -t

it will return a message that the "tseconds" parameter was missing and then display the usage.

Conversely, if someone enters:

psmonitor -n

I want to return a message saying the count parameter is missing.

I tried adding an "if" statement into the case, but it seems that if it sees that the option/argument isn't complete, then it just goes to the default option.

Any thoughts on how I can make this work?

Thanks.

Jason

Recommended Answers

All 5 Replies

#!/bin/bash
# This bash script will locate and replace spaces
# in the filenames
DIR="."
# Controlling a loop with bash read command by redirecting STDOUT as
# a STDIN to while loop
# find will not truncate filenames containing spaces
find $DIR -type f | while read file; do
# using POSIX class [:space:] to find space in the filename
if [[ "$file" = *[[:space:]]* ]]; then
# substitute space with "_" character and consequently rename the file
mv "$file" `echo $file | tr ' ' '_'`
fi;
# end of while loop
done

<<snip>>

#!/bin/bash

mv "$file" `echo $file | tr ' ' '_'`


If you're using bash, you don't need tr:

mv "$file" "${file// /_}"

(It would be a god idea to check whether a file with the new name already exists.)

commented: Neat trick.. :) +4
tseconds=default
count=default

while getopts t:n: opt
do
  case $opt in
    n) count=$OPTARG ;;
    t) tseconds=$OPTARG ;;
  esac
done
shift "$(( $OPTIND - 1 ))"

I'm writing a shell script that can take zero to 2 user arguments.

I have an if statement to check to see if any arguments were entered and then I have a case to enter the arguments into the correct variables. This all works great.

Now, I want to add checking to see if someone adds the argument without a parameter, and then return an appropriate message.

For example, this is the script usage:

psmonitor [-t tseconds] [-n count]

I want checking to so that if someone enters:

psmonitor -t

it will return a message that the "tseconds" parameter was missing and then display the usage.

Conversely, if someone enters:

psmonitor -n

I want to return a message saying the count parameter is missing.

I tried adding an "if" statement into the case, but it seems that if it sees that the option/argument isn't complete, then it just goes to the default option.

Any thoughts on how I can make this work?

Thanks.

Jason

I just tried the script as you mentioned.This will do the following for you.

if [[ $# -eq 0 || $# -gt 2 ]];then
echo "psmonitor [-t tseconds] [-n count]"
elif [[ $# -eq 1 ]];then
if [[ $1 == "-t" ]];then
echo "tseconds parameter was missing"
else
echo "count parameter was missing"
fi
fi
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.