i dont know why it writed me 11:not found - bash

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

Related

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

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?

unable to do ssh to remote server gettign banner error

#!/bin/bash
FILE="${1}"
while read line; do
REMOTESERVER=`echo ${line} | cut -d"," -f1`
REMOTEUSER=`echo ${line} | cut -d"," -f2`
REMOTEPASS=`echo ${line} | cut -d"," -f3`
DBUSER=`echo ${line} | cut -d"," -f4`
DBPASS=`echo ${line} | cut -d"," -f5`
DBNAME=`echo ${line} | cut -d"," -f6`
output=`sshpass -p \'$REMOTEPASS\' ssh -o StrictHostKeyChecking=no -T $REMOTEUSER#$REMOTESERVER
sleep 5
sqlplus -s "$DBUSER/$DBPASS#$DBNAME " <<EOF
set heading off feedback off verify off
select Instance_name,host_name,version,startup_time,status from v\\$instance;
exit
EOF
echo $output >> dboutput.txt

how to read words from a file using shell script

i have a file /ws/$1-rcd/temp.txt which has only one line as follows
198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/...
i have a script to get the value repository/open_source/commons_collections and 3_2_2 by reading the file and looping through it using for loop
i have my code as follows
grep -n "$4" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f1,2 | sed -e 's/\:element/|/g' | sed -e 's/ //g' > /ws/$1-rcd/temp.txt
for i in `cat /ws/$1-rcd/temp.txt`
do
line=`echo $i | cut -d"|" -f1`
path=`echo $i | cut -d"|" -f2`
whoami
directory_temp=`echo $path | awk -F "/" '{ print $(NF-2)}'`
if [ "$directory_temp" == "$4" ]
then
OLD_VERSION=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print $(NF-1)}'`
total_fields=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print NF }'`
dir_path=`expr ${total_fields} - 2`
loc=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | cut -d"/" -f1-"${dir_path}"`
location=`echo $loc | cut -d"/" -f4,5,6`
fi
done
but when i run this code it gives me an error as
-bash: line 45: syntax error near unexpected token |'
-bash: line 45:for i in 198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/...'
can anyone please suggest what am i doing wrong
If you want to iterate through each line of a file, use while loop like below
while read -r line ;do
echo $line
done <file.txt
so, your code can be rewritten as
grep -n "$4" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f1,2 | sed -e 's/\:element/|/g' | sed -e 's/ //g' > /ws/$1-rcd/temp.txt
while read i ; do
line=`echo $i | cut -d"|" -f1`
path=`echo $i | cut -d"|" -f2`
whoami
directory_temp=`echo $path | awk -F "/" '{ print $(NF-2)}'`
if [ "$directory_temp" == "$4" ]
then
OLD_VERSION=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print $(NF-1)}'`
total_fields=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print NF }'`
dir_path=`expr ${total_fields} - 2`
loc=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | cut -d"/" -f1-"${dir_path}"`
location=`echo $loc | cut -d"/" -f4,5,6`
fi
done < /ws/$1-rcd/temp.txt
You may be better served relying on parameter expansion and substring removal. For example:
#!/bin/sh
a=$(<dat/lline.txt) ## read file into a
a=${a##*ccm_tpl/} ## remove from left to ccm_tpl/
num=${a##*collections/} ## remove from left to collections/
num=${num%%/*} ## remove from right to /
a=${a%%${num}*} ## remove from right to $num
Input File
$ cat dat/lline.txt
198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/..
Output
$ sh getvals.sh
a : repository/open_source/commons_collections/
num : 3_2_2
If you need to trim in some other way, just let me know and I'm happy to help further.

Resources