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}'`
Related
I am running below script and getting
error script.sh: line 9: unexpected EOF while looking for matching `''
script.sh: line 15: syntax error: unexpected end of file.
Though I tried to run line 9 manually n it runs without error.
alias gxt="awk -F "_" '{print \$1}' test | uniq"
count = $(cat test | awk -F "_" '{print $1} | uniq | wc -l)
for i in {1..count};
do
User=$(gxt | head -n $i)
recharge=$(grep -E "$User.recharge" test| awk -F "_" '{print $3}' | xargs )
total1=( $((${recharge// /+})))
sales=$(grep -E "$User.sale" test| awk -F "_" '{print $3}' | xargs )
total2=( $((${sales// /+})))
balance=`expr $total1 - $total2`
echo $User.balance.$balance >> result
done
Other than the issues already reported and those exposed by shellcheck, there is another issue:
for i in {1..count};
'count' cannot be a variable. It can only be a constant.
Change it to
for ((i = 1; i <= count; i++)); do whatever ; done
count=$(cat test | awk -F "_" '{print $1}` | uniq | wc -l)
missing ' at the end of {print $1}
Inadvertently added spaces around =
This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 5 years ago.
I have a Error Log file with contents as -
2017/11/06 13:17:05 [notice] 18164#18164: signal process started
.
.
.
I have command that will extract the date and notice,warn message
cat whole_error.log | cut -d" " -f1,3,5-20 | sort -nr | grep "warn\|notice" | awk '$1 >= 2017/09/02 && $1 <= 2017/11/06' | awk '{print $1 " " $2" " $3}'
Its working fine entirely, i am getting the expected output
But, i want to take the start date and end date as command line argument input
and for that, i wrote the script as -
#!/bin/bash
file_name=$1
start_date=$2
end_date=$3
cat $file_name | cut -d" " -f1,3,5-20 | sort -nr | grep "warn\|notice" | awk '$1 >= $start_date && $1 <= $end_date' | awk '{print $1 " " $2" " $3}'
But its not returning anything. No error message nothing..Just the prompt arrives again.
How to fix this..
Use -v to pass shell variables into awk:
#!/bin/bash
file_name=$1
start_date=$2
end_date=$3
<"$file_name" cut -d" " -f1,3,5-20 \
| sort -nr \
| awk -v start_date="$start_date" -v end_date="$end_date" \
'/warn|notice/ && $1 >= start_date && $1 <= end_date {print $1 " " $2" " $3}'
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.
I have the following script:
#!/bin/bash
EXTENT=`ogrinfo -so area.geojson ogrgeojson | grep Extent | sed 's/Extent: //g' | sed 's/(//g' | sed 's/)//g' | sed 's/ - /, /g'`
echo $EXTENT
EXTENT=`echo $EXTENT | awk -F ',' '{print $1 " " $4 " " $3 " " $2}'`
echo $EXTENT
and for some reason it eats up some of the numbers and output:
12.834778, 52.014472, 13.610687, 52.399905
13.610687 52.014472
where expected on the second line is the 4 numbers.
The thing that I do not understand is that, if i copy paste the 4 commands myself to terminal everything works as expected. What do i need to do to make it work in a bash script?
The only way I found to reproduce your problem was this:
$ echo $'12.834778\r, 52.014472\r, 13.610687\r, 52.399905' |
awk -F ',' '{print $1 " " $4 " " $3 " " $2}'
52.014472 13.610687
Please remove the "carriage returns" in your file and you should be fine.
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