I am having trouble writing a bash script that saves each line of the output from the command #mount | grep ^\/dev/
into a variable.
Below is my bash script.
#!/bin/bash
mount | grep ^\/dev/ > tempoary
input=$(cat tempoary)
x=0
while IFS= read -r line
do
x=$((x+1))
echo "$line" > /tmp/directory/$x
for file in $(echo "$line"); do
eval "var$x=$file";
echo "$file"
done
done <<< "$input"
Output below:
[root@localhost tmp]# sh script
/dev/mapper/rhel-root
on
/
type
xfs
(rw,relatime,seclabel,attr2,inode64,noquota)
/dev/sda1
on
/boot
type
xfs
(rw,relatime,seclabel,attr2,inode64,noquota)
I expect to have output format like this:
[root@localhost tmp]# sh script
/dev/mapper/rhel-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
Can you give me some pointers to how I can achieve this?
PS. Double quoting "$(echo "$line")"
won't help, it would only prompt syntax error below.
[root@localhost tmp]# sh script
script: eval: line 14: syntax error near unexpected token `('
script: eval: line 14: `var1=/dev/mapper/rhel-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)'
mount | grep ^\/dev/
will give the sort of output you want. So all this processing is for some other reason. What is it you're trying to do? After all,mount | grep ^\/dev/ | while read -r line; do echo "$line"; done
is a simple loop that displays the same thing.