Script: how to assign values to variables using "awk" results?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,629
Rep:
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?
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,629
Original Poster
Rep:
Yep, works. After some thought, I even remembered why , 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...).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.