Reputation: 1016
I have a program that will take user input string and create output files accordingly, for example, "./bashexample2 J40087" this will create output files for all the files in the folder that contain the string J40087. One problem is that if the user does not input anything in the input string it will generate output files for every file inside the containing folder. Is there a way to prevent user to input nothing in the input string? Or maybe spit out some sort of warning saying " please input an input string".
#Please follow the following example as input: xl-irv-05{kmoslehp}312: ./bashexample2 J40087
#!/bin/bash
directory=$(cd `dirname .` && pwd) ##declaring current path
tag=$1 ##declaring argument which is the user input string
echo find: $tag on $directory ##output input string in current directory.
find $directory . -maxdepth 0 -type f -exec grep -sl "$tag" {} \; ##this finds the string the user requested
for files in "$directory"/*"$tag"* ##for all the files with input string name...
do
if [[ $files == *.std ]]; then ##if files have .std extensions convert them to .sum files...
/projects/OPSLIB/BCMTOOLS/sumfmt_linux < "$files" > "${files}.sum"
fi
if [[ $files == *.txt ]]; then ## if files have .txt extensions grep all fails and convert them..
egrep "device|Device|\(F\)" "$files" > "${files}.fail"
fi
echo $files ##print all files that we found
done
Upvotes: 2
Views: 1533
Reputation: 12575
You can use $# to know how many arguments has been passed as parameters and then ask if there is at least one argument.
For example
if [ $# -gt 0 ]; then
... your logic here ...
As a note apart, you can read the first parameter passed to your script using $1, and $2 for the second one, and so on.
Hope that helps.
Upvotes: 0
Reputation: 291
I would do something like this:
tag=$1
if [ -z "$tag" ]; then
echo "Please supply a string"
exit 1
fi
Upvotes: 3