LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Script: how to assign values to variables using "awk" results? (https://www.linuxquestions.org/questions/linux-software-2/script-how-to-assign-values-to-variables-using-awk-results-683998/)

JZL240I-U 11-17-2008 05:29 AM

Script: how to assign values to variables using "awk" results?
 
I want to write a script to find out the journal size of an ext3 file system. I have two commands (graciously provided by unSpawn):
Code:

The journal is located at inode:
'tune2fs -l /dev/device | awk '/Journal inode/ {print $3}''

The size is:
debugfs -R "stat <inodenumber>" /dev/device 2>&1| awk '/Size: / {print $6}'|head -1

These commands work okay from the command line. I wanted to do something like
Code:

#!/bin/bash
#first parameter is the device name [s|h]dxy
 
inode='tune2fs -l /dev/device | awk '/Journal inode/ {print $3}''
#and noticed that that won't work.
# nor will
echo "Journal Size: "
debugfs -R "stat <inode>" /dev/$1 2>&1| awk '/Size: / {print $6}'|head -1 >1

Hm. So how can I get the value awk is printing in the first statement loaded into a variable (e.g. inode), and how would the second command print the journal size to the screen?

http://www.tldp.org/LDP/Bash-Beginne...e.html#chap_03 left me stumped...

chrism01 11-17-2008 05:34 AM

Use backquotes(`) not single quotes(') to run a cmd and save the result thus:

var=`cmd`
echo $var

JZL240I-U 11-17-2008 05:37 AM

Aha. I'll try that this evening and be back with the result.

Anybody about why printing the results from within the script doesn't work? Can it be done less clumsily?

JZL240I-U 11-18-2008 12:59 AM

Yep, works. After some thought, I even remembered why :rolleyes:, thanks chrism01.

I solved the other question by the same method, i.e. put the statement in back ticks and assigned the value to a variable (yes, I know I could put them out with "echo" but later on I want to calculate the byte value into MBs, so...).


All times are GMT -5. The time now is 10:21 AM.