How can i put command output into a variable [duplicate] - bash

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
Trying to run the following bash script. The output of the following ling fails:
mem = free | grep Mem | awk '{print $4/$2 * 100}'
With the error:
mem: command not found

Like this:
mem=$(free | grep Mem | awk '{print $4/$2 * 100}')
You can also use backticks:
mem=`free | grep Mem | awk '{print $4/$2 * 100}'`
But parentheses are preferred now. More: http://mywiki.wooledge.org/BashFAQ/082

Related

Passing two arguments to another command in the same line [duplicate]

This question already has answers here:
Is it possible to insert arguments at different points in a command in linux while piping
(2 answers)
Closed 9 days ago.
I need to pass two arguments to the same command. The output of the first command look like this:
BLA BLE
A1 B2
A2 B3
Right now I using this for using the first argument
firstCommand | tail -n1 | awk '{print $1}' | xargs -I_ secondCommand _
But the secondCommand has changed and right now I need to pass a second argument
secondComman A --second B
Is this posssible using xargs?
I try multiple stackoverflow solution, but none with was succesful.
Add printing 2nd argument to awk and capture 2 arguments with xargs:
firstCommand | tail -n1 | awk '{print $1,$2}' | xargs -n2 sh -c 'secondCommand "$1" --second "$2"' _

Bash script doesn't show output, but the exact same script in a oneliner does [duplicate]

This question already has answers here:
Printing output of C program in shell script
(1 answer)
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 7 months ago.
This bash script returns nothing, but the exact same script ran in a oneliner returns the correct output.
#!/bin/bash
URL="localhost:3000"
content=$(curl -s $URL | sed -E 's/<[^>]*>//g')
vidsWatched=$(echo $content | grep -A 3 "Videos Watched" | sed -n 4p)
echo "$vidsWatched"
Vs the onliner:
curl -s "http://localhost:3000" | sed -E 's/<[^>]*>//g' | grep -A 3 "Videos Watched" | sed -n 4p
Output:
600

Can't assign linux command output into a variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I've tried to write the following code in bash but I am not able to get the output to be echoed.
part1="blkid | grep -P 'CENTOS 7' | cut -c1-9"
echo "$part1"
Try this
part1=$(blkid | grep -P 'CENTOS 7' | cut -c1-9)
echo "$part1"

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

How to store output as variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I'm looking to store the hash of my most recently downloaded file in my downloads folder as a variable.
So far, this is what I have:
md5sum $(ls -t | head -n1) | awk '{print $1}'
Output:
user#ci-lux-soryan:~/Downloads$ md5sum $(ls -t | head -n1) | awk '{print $1}'
c1924742187128cc9cb2ec04ecbd1ca6
I have tried storing it as a variable like so, but it doesn't work:
VTHash=$(md5sum $(ls -t | head -n1) | awk '{print $1}')
Any ideas, where am I going wrong
As #Cyrus outlined parsing ls has its own pitfalls and therefore better to avoid it altogether rather than allowing unexpected corner cases. The following shall facilitate the requirements epitomised.
VTHash="$(find -type f -mtime 0 | tail -n 1 | xargs md5sum | awk '{ print $1 }')"

Resources