Figuring out a syntax error on my first shell script - bash

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

Related

i dont know why it writed me 11:not found

it writes me "file name" 18:11 not found
line 18 is where the if statement starts
#!/bin/bash
westfunc() {
echo "please select 2 airports(example:jfk):\n"
read place
read place2
echo "\e[32m--------------------------------------------------------------------------------"
echo
echo " $place $place2"
de=`weather-util -m $place | grep "Temperature"| cut -d "T" -f2 | cut -d "e" -f4| cut -d ":" -f2`
we=`weather-util -m $place | grep "Wind" | cut -d "W" -f3 | cut -d ")" -f2`
sc=`weather-util -m $place | grep "Sky conditions" | cut -d "n" -f3 | cut -d ":" -f2`
var=`weather-util -m $place | grep "Temperature"| cut -d " " -f5 | cut -d "." -f1`
if (( $var > 5 ))
then
echo "hello"
fi
}
i tried to change $ the ( and stuff like that

How to fix "syntax error near unexpected token `done' " in a nested loop in 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

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

How to hide error & warning bash colors in Xcode and Source Tree?

I have following bash script
while read -r line; do
FILEPATH=$(echo $line | cut -d : -f 1)
L=$(echo $line | cut -d : -f 2)
C=$(echo $line | cut -d : -f 3)
TYPE=$(echo $line | cut -d : -f 4 | cut -c 2-)
MESSAGE=$(echo $line | cut -d : -f 5 | cut -c 2-)
DESCRIPTION=$(echo $line | cut -d : -f 6 | cut -c 2-)
if [ "$TYPE" == 'error' ]; then
printf "\n \e[31m$TYPE\e[39m\n"
else
printf "\n \e[33m$TYPE\e[39m\n"
fi
printf " \e[90m$FILEPATH:$L:$C\e[39m\n"
printf " $MESSAGE - $DESCRIPTION\n"
done <<< "$RESULT"
In terminal colors works perfectly.
How to hide colors in Xcode & Source Tree?

bash script, ask for arg if not provided

I have created a script that will check to see if a user you provide is logged on and display the duration of the session if logged on. What i need to do now is if no argument (username) is provided when the command is issued, ask for one and have the same results as if you have provided one.
Here is what I have:
name=$(cat /etc/passwd | grep $1 | cut -d':' -f5 | tr ':' ' ' | sed 's/,//' | sed 's/^\([^ ]*\) \([^ ]*\)/\2 \1/' | sort -t' ' -k3,3)
terminal=$(who | grep $1 | cut -d' ' -f3)
loginHour=$(who | grep $1 | cut -c30-31)
loginMin=$(who | grep $1 | cut -c33-34)
loginMins=$((loginHour * 60 + loginMin))
nowHour=$(date +%R | cut -c1-2)
nowMin=$(date +%R | cut -c4-5)
nowMins=$((nowHour * 60 + nowMin))
totalMins=$((nowMins - loginMins))
hoursOn=$((totalMins / 60))
minsOn=$((totalMins % 60))
clear
echo
if [[ $# -eq 1 ]] ; then
grep -q $1 /etc/passwd
if grep -q $1 /etc/passwd ; then
clear
echo
if who | grep $1 > /dev/null ; then
echo "$name" is currently logged on to terminal "$terminal" and has been for "$hoursOn" hour"(s)" and "$minsOn" minute"(s)".
echo
exit 0
else
echo "$name" is NOT currently logged on.
echo
exit 1
fi
else
echo The user you entered is not a valid user on this system.
echo
exit 2
fi
fi
I had an attempt before but was not the desired result so I removed it out of confusion.
if [[ $# -eq 0 ]]
then
read -p "Enter Name: " username
else
username=$1
fi
then replace all subsequent references to $1 by $username
You can also abort if no name given
# : does nothing it just forces the evaluation
: ${1:?"Need to provide name to script"}

Resources