How to extract values from a command with multi-line output?
Bo Berglund
bo.berglund at gmail.com
Mon Apr 4 18:40:54 UTC 2022
I am trying to script working with a file system image file after I have found
out how I can do this manually.
When scripting the procedure I have one big problem and that is how I can
extract data from the output of an fdisk command...
I am not versed in the various tools on Linux I have seen mentioned when
searching. Something simple to just extract the numbers is what I need.
I do this to get the info (the lines may be wrapped by the newsreader):
----
$ fdisk -lu filename.img | tail -n 2
filename.img1 8192 532479 524288 256M c W95 FAT32 (LBA)
filename.img2 532480 3923967 3391488 1.6G 83 Linux
----
The response from this command is two lines (one for each partition in the
image) and I am only interested in the 3 numbers on the two lines:
8192 524288 532480
These are the 1st and 3rd number of the first line and the first number on the
second line, following the file name.
How can I get these into 3 variables inside the script?
I use them in order to mount the images:
sudo mount -t ext4 -o loop,offset=$((532480*512)) filename.img /mnt/disk
sudo mount -t vfat -o loop,offset=$((8192*512)),sizelimit=$((524288*512))
filename.img /mnt/disk/boot
I want to automate this mount operation in a "mountimage" script, so how to
extract the numbers from the response shown?
--
Bo Berglund
Developer in Sweden
More information about the ubuntu-users
mailing list