0

I am writing a bash code and want to print the two variables values into same line, while i am trying the same with echo command I'm not getting that as those are getting printed one after another.

#!/bin/bash
BLK_ID=$(blkid | awk -F:  '{print $1}')
for i in "$BLK_ID";
  do
BLK_VAL=$(blkid -s UUID -o value $i);

#echo "Disk:${BLK_ID} -- UUID:${BLK_VAL}"
echo "$BLK_ID $BLK_VAL"
done

Result

/dev/sdb1
/dev/sda1
/dev/sda2
/dev/sda4

Desired :

/dev/sdb1    8ce464b0-cc26-41ae-b6a5-2643b3e09226
/dev/sda1    LLi8wj-ROs5-IRQT-5qHy-3jDg-snro-RYbvOo
/dev/sda2    45y8wj-ROs5-IRQT-5qHy-4jDg-snro-RYbvOo
/dev/sda4    278edab7-9ccd-4302-bf84-5a1a68e897d2
1
  • Does the answer below helps you? if yes then please accept the answer. Happy Learning. Commented Sep 7, 2020 at 11:23

2 Answers 2

5

Try below :

paste <(printf %s "$BLK_ID") <(printf %s "$BLK_VAL") 

OR

paste <(printf %s "$BLK_ID") <(printf %s "$BLK_VAL") | column -t  <-- this will help to better align columns

Output:

/dev/sdb1    8ce464b0-cc26-41ae-b6a5-2643b3e09226
/dev/sda1    LLi8wj-ROs5-IRQT-5qHy-3jDg-snro-RYbvOo
/dev/sda2    45y8wj-ROs5-IRQT-5qHy-4jDg-snro-RYbvOo
/dev/sda4    278edab7-9ccd-4302-bf84-5a1a68e897d2
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Karn Kumar for the nice trick that worked nicely.
2

You don't need for loop or awk, you can just use this

#!/bin/bash
BLK_ID=$(blkid -o device)
BLK_VAL=$(blkid -s UUID -o value);
paste <(printf %s "$BLK_ID") <(printf %s "$BLK_VAL") 

The for loop you have in your example will only run once, which is why you have all the devices and then all the uuids.

You have declared BLK_ID as a single argument with one value holding all devices separated by space and i in for loop only takes that one value and loop runs only once, because you used double quotes around the variable.

What you have done in the first line is basically said

BLK_ID="/dev/sdb1 /dev/sda1 /dev/sda2 /dev/sda4"

If you use it in for loop i will not iterate through all the elements since you used double quotes around the variable, if you want to iterate through each device in the variable don't use quotes around it.

Your original code:

#!/bin/bash
BLK_ID=$(blkid | awk -F:  '{print $1}')
for i in "$BLK_ID";
  do
BLK_VAL=$(blkid -s UUID -o value $i);

#echo "Disk:${BLK_ID} -- UUID:${BLK_VAL}"
echo "$BLK_ID $BLK_VAL"
done

Your code with minimal changes, using awk and for loop would have to look like this:

#!/bin/bash
BLK_ID=$(blkid | awk -F:  '{print $1}')
for i in $BLK_ID;
  do
BLK_VAL=$(blkid -s UUID -o value $i);

#echo "Disk:${BLK_ID} -- UUID:${BLK_VAL}"
echo "$i $BLK_VAL"
done

But the awk and for loop are unnecessary, you can just use the three lines of code in the beginning of my answer.

1 Comment

Thank you for the detailed explanation and a nice answer @rAlen, this make me learning new things +1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.