unexpected EOF while looking for matching ``' - bash

I try to run this script to create simple, beautiful windows with bushcurses but there is an error
~/bashcurses$ ./myfirstwindow1.sh
./myfirstwindow1.sh: line 32: unexpected EOF while looking for matching ``'
./myfirstwindow1.sh: line 39: syntax error: unexpected end of file
I have tried to add or delete '" but cannot find proper solution. Can you please help me to identify the cause of the error?
script:
#!/bin/bash
. `dirname $0`/simple_curses.sh
main (){
window "`hostname`" "red"
append "`date`"
addsep
append_tabbed "Up since|`uptime | cut -f1 -d"," | sed 's/^ *//' | cut -f3- -d" "`" 2 "|"
append_tabbed "Users:`uptime | cut -f2 -d"," | sed 's/^ *//'| cut -f1 -d" "`" 2
append_tabbed "`awk '{print "Load average:" $1 " " $2 " " $3}' < /proc/loadavg`" 2
endwin
window "Memory usage" "red"
append_tabbed `cat /proc/meminfo | awk '/MemTotal/ {print "Total:" $2/1024}'` 2
append_tabbed `cat /proc/meminfo | awk '/MemFree/ {print "Used:" $2/1024}'` 2
endwin
window "Processus taking memory and CPU" "green"
for i in `seq 2 6`; do
append_tabbed "`ps ax -o pid,rss,pcpu,ucmd --sort=-cpu,-rss | sed -n "$i,$i p" | awk '{printf "%s: %smo: %s%%" , $4, $2/1024, $3 }'`" 3
done
endwin
window "Last kernel messages" "blue"
dmesg | tail -n 10 > /tmp/deskbar.dmesg
while read line; do
append_tabbed "$line" 1 "~"
done < /tmp/deskbar.dmesg
rm -f /tmp/deskbar.dmesg
endwin
window "Inet interfaces" "grey"
ERROR >> _ifaces=$(for inet in `ifconfig | cut -f1 -d " " | sed -n "/./ p"`; do ifconfig $inet | awk 'BEGIN{printf "%s", "'"$inet"'"} /adr:/ {printf ":%s\n", $2}'|sed 's/adr://'; done)
for ifac in $_ifaces; do
append_tabbed "$ifac" 2
done
endwin
}
main_loop 1

Related

Variable works outside function, inside function give syntax error

I've been trying to get this function to work. I'm on a 17" MacBook Pro Early 2011. Setting all the variables under "else" work great if you run them separately, they also echo properly. For some reason when I put them in the function… I get a syntax error on line 12 and
battery ()
{
BATTERYISPRESENT=`ioreg -l | grep Cycle`
if [[ $BATTERYISPRESENT != *'Cycle' ]]
then
echo "No Battery Present, Probably a desktop Mac."
else
BATTERYCYCLES=`system_profiler SPPowerDataType | grep "Cycle Count" | awk '{print $3}'`
BATTCURRCAP=`pmset -g batt | sed -n '2 p' | awk '{ print $2 }' | sed 's/;//g'`
BATTERYCHARGESTATUS=`system_profiler SPPowerDataType | grep "Charging" | awk '{ print $2 }'`
BATTERYISCHARGING=`system_profiler SPPowerDataType | grep -A 4 "AC Charger Information" | grep "Connected: " | awk '{ print $2 }'`
CHARGERISCONNECTED=`system_profiler SPPowerDataType | grep -A 4 "AC Charger Information" | grep "Connected: " | awk '{ print $2 }'`
echo $BATTERYCYCLES
echo $BATTCURRCAP
echo $BATTERYISCHARGING
echo $CHARGERISCONNECTED
}
The output reads:
line 12: unexpected EOF while looking for matching ``'
line 18: syntax error: unexpected end of file
Any assistance would be greatly appreciated.
A fi upon you — you're missing the fi at the end of the else.

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.

how to get only one occurance of a line in a file

i have a file raw-vobs-config-spec where there are two lines
element /vob/ccm_tpl/repository/open_source/ciscossl_fom/4_1/... TPLBASE
element /vob/ccm_tpl/repository/open_source/ciscossl/1_0_2d_5_4/... VERSION_04
i have my code:
OLD_VERSION=`grep "ciscossl" raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print $(NF-1)}'`
echo $OLD_VERSION
total_fields=`grep "ciscossl" raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print NF }'`
echo $total_fields
#directory_path=`grep "ciscossl" raw-vobs-config-spec | cut -d " " -f2 | cut -d"/" -f1-"${total_fields}"`
#echo $directory_path
loc=`grep "ciscossl" raw-vobs-config-spec_new | cut -d " " -f2 | cut -d"/" -f1-6`
echo $loc
so it is printing o/p as
4_1 1_0_2d_5_4
8 8
/vob/ccm_tpl/repository/open_source/ciscossl_fom
/vob/ccm_tpl/repository/open_source/ciscossl
but i need the output as
4_1
8
/vob/ccm_tpl/repository/open_source/ciscossl_fom
how can i get that?
You can use read without while loop to read just one line first and then process the read text using awk:
# read one line from input file
read _ line _ < raw-vobs-config-spec
# process the line with awk
echo "$line" | awk 'BEGIN{FS=OFS="/"} {print $(NF-1) ORS NF; NF-=2; print}'
Output:
4_1
8
/vob/ccm_tpl/repository/open_source/ciscossl_fom

Shell Scripting command not found Error

I'm new to programming with shell and want to ask what is wrong with my code?
#!/bin/bash
#DHT11
SCRIPT="/var/www/ErnestynoFailai/scripts/DHT 11 4"
#DHT22
#SCRIPT="/root/to/folder/DHT 22 4"
#AM2302
#SCRIPT="/root/to/folder/DHT 2302 4"
HUMIDITY=`$SCRIPT | grep "Temp" | awk -F " " '{print $7}'`
TEMPRATURE=`$SCRIPT | grep "Temp" | awk -F " " '{print $3}'`
#-a = AND = &&
while [ $HUMIDITY=="" -a $TEMPRATURE=="" ]
do
$HUMIDITY=`$SCRIPT | grep "Temp" | awk -F " " '{print $7}'`
$TEMPRATURE=`$SCRIPT | grep "Temp" | awk -F " " '{print $3}'`
done
echo "$HUMIDITY"
echo "$TEMPRATURE"
I'm getting:
line 14 or 15 =26: or =: command not found...
There are two problems:
These lines are not returning anything, or at least a string:
HUMIDITY=`$SCRIPT | grep "Temp" | awk -F " " '{print $7}'`
TEMPRATURE=`$SCRIPT | grep "Temp" | awk -F " " '{print $3}'`
This is causing =: command not found errors.
Your while condition needs to be
while [[ $HUMIDITY == "" && $TEMPRATURE == "" ]]
Finally, while not causing problems, TEMPERATURE is misspelled, which might cause you grief later on.
Variables have to be assigned without leading $:
HUMIDITY=`$SCRIPT | grep "Temp" | awk -F " " '{print $7}'`
TEMPRATURE=`$SCRIPT | grep "Temp" | awk -F " " '{print $3}'`

Counting duplicate lines based on user input without showing certain information

Hi i got this short code that i want to use to count the number of duplicate lines but i only wish for the number to be shown
the input of the file
The Hunger Games:Suzanne Collins:1:1:1
The Hunger Games:test:1:1:1
desired output
found: 2
the output with the code below
found: 2 The Hunger Games
Code that was used
echo "Title: "
read title
record=$(grep -io "$title" BookDB.txt | sort | uniq -c)
echo "found: " $record
You can use awk or cut
Awk
echo "found: " $record | awk '{print $1 $2}'
Cut
echo "found: 2 The Hunger Games" | cut -f1-2 -d" "
Test
$ echo "found: " $record | awk '{print $1 $2}'
found: 2
$ echo "found: 2 The Hunger Games" | cut -f1-2 -d" "
found: 2

Resources