linux - Bash while loop `done' syntax error unexpected token -
learning bash scripting in linux on virtualbox.
i'm writing script uses while loop ask text file write if decide not overwrite existing file, among other things.
here's code:
#!/bin/bash bool="true" counter="true" while [ "${bool}" == "true" ] ; bool="false" if [ "${counter}" == "true" ] ; if [ $# -eq 1 ] ; ff=$1 fi else read -p "enter .txt file write to: " ff fi txt=".txt" if [[ $ff != *$txt* ]] ; echo $ff ff="$ff$txt" echo $ff fi if [ -w $ff ] ; var="true" while [ "${var}" == "true" ] ; var="false" read -p "${ff} exists. want overwrite it? y/n: " yorn if [ $yorn == "y" ] ; echo "'$ff' being overwitten" elif [ $yorn == "n" ] ; echo "let's try again..." bool="true" else echo "you entered command other y or n." var="true" fi done else echo "'$ff' has been created" fi counter="false" done echo "writing ${ff}..." echo "${ff}" > $ff echo "" >> $ff declare -a alphabet=("a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z") echo ${alphabet[@]} letters=${#alphabet[@]} echo "there ${letters} letters in alphabet" loops=$((letters*letters*letters)) echo "the script loops ${loops} times" start=$(date +%s.%n) x in "${alphabet[@]}" x=$x$x y in "${alphabet[@]}" y=$y$y z in "${alphabet[@]}" z=$z$z xyz=$x$y$z grep $xyz /usr/share/dict/words >> $ff done done done end=$(date +%s.%n) elapsed=$(echo "$end - $start" | bc -l) echo "the search took ${elapsed} seconds." sleep 10s emacs $ff exit 0
here's error done of while loop:
ubuntu@ubuntu-virtualbox:~/scripts$ ./script.sh abc ./script.sh: line 35: syntax error near unexpected token `done' ./script.sh: line 35: `done'
what's issue here?
you have 2 issues:
1) need space here. general suggestion, it's better use shell built-in[[ ... ]]
on test [ ... ]
in bash.
if [ "${counter}" == "true" ] ; ^
2) not closing if "fi" here.
elif [ $yorn == "n" ] ; echo "let's try again..." bool=true fi # <--- closing "fi" here
if indent code, spot these kind of errors more quickly.
Comments
Post a Comment