Reputation: 55
I'm totally new to writing code with shell script
.
This is my code:
#!/bin/bash
echo -n "Output to $2 "
# set counter
count=1
# zap output file
> $2
# Loop
while [ $count -le $1 ]
do
# generate some random text
randomnumber=`od -A n -t d -N 1 /dev/urandom`
randomtext=`cat /dev/urandom | tr -cd "[:alnum:]" | head -c $randomnumber`
# generate a random number
randomnumber=`od -A n -t d -N 1 /dev/urandom`
# output to file
echo "$count,$randomtext,$randomnumber" | sed -e "s: *::g" >> $2
# increment counter
count=$(($count + 1))
if [ $(($count % 500)) -eq 0 ]
then
echo -n "." fi
done
echo " Output complete"
And this is my error:
Line 2: ambiguous redirect and Line 14: unary operator expected.
Can anybody help me to understand why I having that error?
Upvotes: 1
Views: 328
Reputation: 464
As @GlennJackman points out, the lines are not matching the code, hence I am guessing the following:
truncate -s0 $2
;
before fi
Try the following:
#!/bin/bash
echo -n "Output to $2 "
# set counter
count=1
# zap output file
truncate -s0 $2
# Loop
while [ $count -le $1 ]
do
# generate some random text
randomnumber=`od -A n -t d -N 1 /dev/urandom`
randomtext=`cat /dev/urandom | tr -cd "[:alnum:]" | head -c $randomnumber`
# generate a random number
randomnumber=`od -A n -t d -N 1 /dev/urandom`
# output to file
echo "$count,$randomtext,$randomnumber" | sed -e "s: *::g" >> $2
# increment counter
count=$(($count + 1))
if [ $(($count % 500)) -eq 0 ]
then
echo -n "."
fi
done
echo " Output complete"
Upvotes: 1