I want this function to return one random word if there aren't any command-line arguments.
I am modifying linuxconfig.org's random-word generator to run even when #$ -ne 1.
function random-word {
if [ $# -eq 0 ] ;
then
echo "I only take one argument, dummy"
# previously was exit 0
fi
# Constants
X=0
ALL_NON_RANDOM_WORDS=/usr/share/dict/words
# total number of non-random words available
non_random_words=`cat $ALL_NON_RANDOM_WORDS | wc -l`
# while loop to generate random words
# number of random generated words depends on supplied argument
while [ $X -lt "$1" ]
do
random_number=`od -N3 -An -i /dev/urandom |
awk -v f=0 -v r="$non_random_words" '{printf "%i\n", f + r * $1 / 16777216}'`
sed `echo $random_number`"q;d" $ALL_NON_RANDOM_WORDS
let "X = X + 1"
done
The statement executes, but there's a bash error:
$ bob
I only take one argument, dummy
bash: [: : integer expression expected
How do I fix this so the bash error won't display?
while [ $x -lt "$1" ] still executes when there are no parameters. The if block previously contained an exit to prevent this from happening.
– mrgnw Jul 23 '14 at 00:02x
isn't what you think. What is it? I don't know, since you didn't post that part of the code. Stop wasting our time and post a working example. – Gilles 'SO- stop being evil' Jul 23 '14 at 00:03arg=${1?ERR: I need at least one argument!} set --
at the start of your script. That way you work with one argument, fail if you don't get at least the one, and ignore all others. – mikeserv Jul 23 '14 at 00:20