How to fix "syntax error near unexpected token `done' " in a nested loop in bash? - bash

I am writing a script that will loop through columns to find an instance of a word.
I decided I do it through nested loops and after executing my code, I get this error:
./gallupscript.sh: line 115: syntax error near unexpected token done'
./gallupscript.sh: line 115:done'
Here is the area where my code fails:
token=2 #token is the column number
starter=0
s1="First" ; s2="Second" ; s3="Third" ; s4="Fourth" ; s5="Fifth"
s=s ; a=1
while [ $token -le 6 ]
do
cat gallup.csv | cut -d',' -f"$token" | grep -n $strength1 | cut -d':' -f1 > str1
if [ -s str1 ]
then
for i in $(cat str1)
do
if [[ $i -ne $number && $starter -eq 0 ]]
then
save=$(cat gallup.csv | head -$i | tail +$i | cut -d',' -f1)
s=s ; s+=$a ; starter=1
printf "-- $strength1 --"
printf "${!s} Strength: $save"
elif [[ $i -ne $number && $starter -ne 0 ]]
then
save=$(cat gallup.csv | head -$i | tail +$i | cut -d',' -f1)
printf ", $save"
fi
done
starter=0
a=$((a+1))
token=$((token+1))
echo #new line
done
This code is expected to output the names (in first columns) where the word is matched with the one I am searching for.

You are not closing your if statement, it doesn't have to do with for.
Use the following code instead:
token=2 #token is the column number
starter=0
s1="First" ; s2="Second" ; s3="Third" ; s4="Fourth" ; s5="Fifth"
s=s ; a=1
while [ $token -le 6 ]
do
cat gallup.csv | cut -d',' -f"$token" | grep -n $strength1 | cut -d':' -f1 > str1
if [ -s str1 ]
then
for i in $(cat str1)
do
if [[ $i -ne $number && $starter -eq 0 ]]
then
save=$(cat gallup.csv | head -$i | tail +$i | cut -d',' -f1)
s=s ; s+=$a ; starter=1
printf "-- $strength1 --"
printf "${!s} Strength: $save"
elif [[ $i -ne $number && $starter -ne 0 ]]
then
save=$(cat gallup.csv | head -$i | tail +$i | cut -d',' -f1)
printf ", $save"
fi
done
fi # <------------ add this line
starter=0
a=$((a+1))
token=$((token+1))
echo #new line
done

Related

String comparison from nested for returns always false

The main issue is that i try to parse ls to do a mock "Compare directories" but when i do so since i use nested fors i cant properly compare the results from it since the comparison of two filenames/strings even if they are the same it always returns false
I tried erasing the white characters but no results.
var1=$(ls -l $1 | grep -v ^d | tail -n +2 | tr -s " "| cut -d " " -f 9)
var2=$(ls -l $2 | grep -v ^d | tail -n +2 | tr -s " "| cut -d " " -f 9)
for i in $var1 ; do
i=$(printf "$i" | tr -d '[:space:]')
flag=0
var3=$(ls -l $1 | grep -v ^d | tail -n +2 | tr -s " " | grep $i | cut -d " " -f 5)
for j in $var2 ; do
j=$(printf $j | tr -d '[:space:]')
var4=$(ls -l $2 | grep -v ^d | tail -n +2 | tr -s " " | grep $j | cut -d " " -f 5)
if [ "$i" == "$j" ] ; then
if [ "$var3" != "$var4" ] ; then
flag=1
fi
else
flag=1
fi
done
if [ $flag -eq 1 ] ; then
printf "$i file does not exist on the $2 catalog\n"
printf "It 's size is :$var3 \n"
let Sum=$Sum+$var3
fi
done
This is not a string comparison problem, it's a logic problem.
I wrote you a MCVE that demonstrates the same problem with less code and fewer dependencies:
flag=0
target="hello"
for candidate in "hello" "world"
do
if [ "$target" != "$candidate" ]
then
flag=1
fi
done
if [ "$flag" -eq 1 ]
then
echo "The string was not found"
fi
This prints The string was not found every time, just like your script, even though it's clearly there.
The problem here is that the script requires that ALL files match. It should only require that ANY file matches. The easiest way to fix this is to:
Set flag=1 when a MATCH is found (not a mismatch)
Make flag=1 signify that a match was found (rather than no match was found)
Here's the version which correctly finds the string:
flag=0
target="hello"
for candidate in "hello" "world"
do
if [ "$target" = "$candidate" ]
then
flag=1
fi
done
if [ "$flag" -eq 1 ]
then
echo "The string was found"
else
echo "The string was not found"
fi

Figuring out a syntax error on my first shell script

I'm completely new to coding and this is my first script. I've been receiving this error from my code and can't seem to figure out what I'm doing wrong.
| LAST NAME | FIRST NAME | JOB | OLD | NEW | MESSAGE
: line 25: syntax error near unexpected token `|'
: line 25: ` else | '
The script is just supposed to read a text file. II've attempted to use gedit to see my mistake and all I've noticed is my JOB= line is discolored.
#!/bin/bash
printf "| % -15s | % -10s | % -6s | % -6s | % -6s | % -40s\n" "LAST NAME" "FIRST NAME" "JOB" "OLD" "NEW" "MESSAGE"
while read line
do
USER ID=` echo $line | cut -d '|' -f1 `
LAST NAME=` echo $line | cut -d '|' -f2 `
FIRST NAME=` echo $line | cut -d '|' -f3 `
JOB=` echo $line | cut -d '|' -f4 `
OLD NICE=` echo $line | cut -d '|' -f5 `
PREFERRED PASSWORD=` echo $line | cut -d '|' -f6 `
FULL NAME="$FIRST_NAME $LAST_NAME"
if [ "$JOB" == "P" ]
then
NEW_NICE=3
elif [ "$JOB" == "S" ]
then
NEW_NICE=6
else |
NEW_ICE=7
fi
test -e /home/$USER_ID
if [ "$?" == "0" ]
then
MESSAGE="$USER_ID already exists"
else
ENCRYPTED_PASSWORD=` echo "$PREFERRED_PASSWORD" | openssl passwd -1 -stdin `
useradd -m -c "$FULL_NAME" -p $ENCRYPTED_PASSWORD $USER_ID
mkdir "/home/$USER_ID/${LAST_NAME}_backup"
chown -r $USER_ID "/home/$USER_ID/${LAST_NAME}_backup"
echo "$USER_ID created"
fi
printf "| % -15s | % -10s | % -6s | % -6s | % -6s | % -40s\n" "$LAST NAME" "$FIRST NAME" "$JOB" "$OLD" "$NEW" "$MESSAGE"
done < test3_data.txt

How to fix an unexpected end of file error in bash?

The code saying that there is "unexpected end of file" error in line 16. Could someone please tell me my mistake?
#!/bin/bash
total=0
for i in `grep 01/Oct/2006 log.txt | cut -d' ' -f1 | sort | uniq -c | sort -n | tail`;
do if [[ $i =~ ^[0-9]+$ ]]; then
total=$(( $total + $i )); fi
for i in `grep 01/Oct/2006 log.txt | cut -d' ' -f1 | sort | uniq -c | sort -rn | head -10 | tr -s ' ' | cut -d' ' -f2,3 | sed -E 's/(\S*) (\S*)/\2 - \1/' | nl -s'. '`;
do
if ! [[ $i =~ ^[0-9]+$ ]];
then
printf $i;
printf " ";
else
printf " $i - $(echo "scale=0; $i * 100 / $total" | bc )%% \n" ;
fi
done
Your first for loop lacks a done.
Here's a working version with improved formatting (but with all original flaws and bugs left inside, I just fixed the one issue asked for here):
#!/bin/bash
$total;
for i in $(
grep 01/Oct/2006 log.txt |
cut -d' ' -f1 |
sort |
uniq -c |
tail);
do
if [[ $i =~ ^[0-9]+$ ]]
then
$total += $i
fi
done
for i in $(
grep 01/Oct/2006 log.txt |
cut -d' ' -f1 |
sort |
uniq -c |
sort -rn |
head -10 |
tr -s ' ' |
cut -d' ' -f2,3 |
sed -E 's/(\S*) (\S*)/\2 - \1/' |
nl -s'. ')
do
if ! [[ $i =~ ^[0-9]+$ ]];
then
printf " $i - ";
else
printf " $i - 0$(echo "scale=0; $i / $total" | bc)%% " ;
fi
done

bash with cut to get a value with carriage return \r

Consider the following bash script
NP=`grep nprocshared $GF | cut -d '=' -f2`
echo $NP
if [ $N -ne $NP ]; then
echo "Error"
Please note the $N is an integer and it is fine! The file contains
%nprocshared=6
%mem=12GB
...
With the set -x option, I see this in the output
++ grep nprocshared file.gjf
++ cut -d = -f2
+ NP=$'6\r'
+ echo $'6\r'
6
+ '[' 4 -ne $'6\r' ']'
So, I expect 4ne6, but I don't see that. What it reads the 6 as 6\r?
NP=`grep nprocshared $GF | cut -d '=' -f2 | tr -d \\r`
echo $NP
if [ $N -ne $NP ]; then
echo "Error"
VoilĂ .
Or, even better, since NP is always a number:
NP=`grep nprocshared $GF | tr -dc 0-9`
echo $NP
if [ $N -ne $NP ]; then
echo "Error"

Converting multiple lines of bash in to a single line

Is there any short and easy way to convert multiple lines of script in to a single line to be parsed in a eval command?
ie
getent group | cut -f3 -d":" | sort -n | uniq -c |\
while read x ; do
[ -z "${x}" ] && break
set - $x ; if [ $1 -gt 1 ]; then
grps=`getent group | nawk -F: '($3 == n) { print $1 }' n=$2 | xargs` ; echo "Duplicate GID ($2): ${grps}" ; fi done
one_line=`cat your_script_file | sed ":a s/[\]$//; N; s/[\]$//; s/\n/ /; t a ;"`
echo $one_line

Resources