Error while trying to pass awk result to variable [duplicate] - bash

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 8 years ago.
I'm trying to create a simple bash script to get the HTTP codes from CURL
So here is my code :
#!/bin/bash
AWKRESULT = $(curl -sL -w "result=%{http_code}" "http://192.168.8.69:8080/myReport/archive" -o "/tmp/reportlog" | awk -F= '{print $2}')
echo $AWKRESULT
the result of
curl -sL -w "result=%{http_code}" "http://192.168.8.69:8080/myReport/archive" -o "/tmp/reportlog" | awk -F= '{print $2}'
is 500.
However it's always has this result :
./test.sh[2]: AWKRESULT: not found.
any idea what am I missing?

Remove the spaces around the =:
AWKRESULT=$(...)

Related

Bash unable to assign value [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
Got a bit of a problem. I'm new to bash and I'm trying my hardest, but I can't figure out how to assign the desired output to the variable. Running this command in the prompt
wc -l data.txt | awk '{ print $1 }'
yields the result 12, which is desired. However if I put it in the test.sh file, it won't work. I've tried different quotations, but all I've managed to get is the entire line as a string...
Test.sh
#! /bin/bash
# Count lines for data.txt files
data1=wc -l data.txt | awk '{ print $1 }'
echo "Lines in data.txt: $data1"
exit
I think you want:
data1="$(wc -l data.txt | awk '{ print $1 }')"
The $() syntax causes bash to execute that expression and replace it with the results.
Actually, powershell does allow you to do a straight = assignment like you did...

how to pass parameters to awk in a script file (using it to replace)? [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 2 years ago.
I would like to use awk to replace any occurrence of a given word with another word in a .sh
The script I wrote is;
#!/bin/bash
read -p "Enter the word to change: " wrd1
read -p "Enter the new word: " wrd2
awk '{sub(/"$wrd1 /, $wrd2")}1' file.txt
but it's not working.
Use the -v option.
awk -v w1="$wrd1" -v w2="$wrd2" '{sub(w1, w2)}1' file.txt
A demonstration:
$ awk -v w1="$wrd1" -v w2="$wrd2" '{sub(w1, w2)}1' <<EOF
foo=3
hi=9
bye=2
EOF
bar=3
hi=9
bye=2

substraction of variable having float value is not working [duplicate]

This question already has answers here:
Floating-point arithmetic in UNIX shell script
(4 answers)
Closed 3 years ago.
I am executing below as part of bash script on solaris 10.
MEM_USED_PC=`prstat -Z 1 1 | grep -i sz | awk '{print $5}' | sed 's/%//'`
MEM_TOTAL_PC=100
MEM_FREE_PC=$(($MEM_TOTAL_PC-$MEM_USED_PC))
but echo $MEM_FREE_PC gives below error:
100-6.5: syntax error: invalid arithmetic operator (error token is ".5")
What could be the problem?
You can use the calculator CLI, bc
MEM_FREE_PC=$(echo "$MEM_TOTAL_PC - $MEM_USED_PC" | bc)
echo $MEM_FREE_PC
Since bash doesn't support floating point, you need something like awk to compute the result:
$ MEM_TOTAL_PC=100
$ MEM_USED_PC=99.33
$ MEM_FREE_PC=$(awk -v MEM_TOTAL_PC=$MEM_TOTAL_PC -v MEM_USED_PC=$MEM_USED_PC 'BEGIN {print MEM_TOTAL_PC-MEM_USED_PC}')
$ echo $MEM_FREE_PC
0.67

bin sh script - "(" unexpected [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
What is wrong with this script?
#!/bin/sh
for file in *.p; do awk -f get_p.awk "$file" > "$file"er; done
awk -f phase-comp.awk file1 file.per # výpočet fází
paste <(awk '{print $1}' output1) <(awk '{print $2}' file1) > out
The error is:
5: skript: Syntax error: "(" unexpected
When I write command separately to terminal. It works well. Thank you
The problem is your shebang:
#!/bin/sh
It means this script is meant to be interpreted by sh, but your script uses process substitution which is a bash-specific feature. So, you should change it to:
#!/bin/bash
to make your script work.

How to pass variable to awk [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using awk with variables
The following command is wrong, the point is I want to use $curLineNumber in awk, how can I do it? Any solution?
curLineNumber = 3
curTime=`ls -l | awk 'NR==$curLineNumber {print $NF}'`
Thanks
curTime=$(ls -l | awk -v line=$curLineNumber 'NR == line { print $NF }'
The -v option is used to specify variables initialized on the command line. I chose the name line for the awk variable.

Resources