LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2013, 05:20 AM   #1
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Pipe within variable for subshell with $()


Hi folks,

im writing a bash script that hdparms some block devices. I want to use a variable like CMD="lsblk /dev/sda | grep -v sda" to only get the partitions of the devices. But bash always ignores the pipe symbol and so lsblk complains about unkown option -v. Any idea how I can go about this?

Code:
#!/bin/bash
#set -x

# Get list of device
CMD="lsblk -e "11,1" -n -l -i -d -o NAME"
read -n 1 -p "Gather all block device data (y/n): " line
echo ""
if [ "$xline" == "xy" ]; then
        echo > ./LAST
else
        x=0
        for i in $($CMD); do
                x=$((x+1))
                echo "$x) $i"
        done
        read -n 1 -p "Gather data of partition from ($(seq -s '/' 1 $x)): " line
        echo ""
        DEV=$($CMD | awk "NR==$line { print $1 }")
        #CMD="lsblk -n -l -i -o NAME /dev/$DEV \| grep -v $DEV$"
        CMD='lsblk -n -l -i -o NAME /dev/'
        CMD+="$DEV "
        CMD+='| grep -v "'
        CMD+="$DEV"
        CMD+='$"'
fi

set -x
echo $CMD
for i in $($CMD); do
        echo some
        echo $i
done

set +x
exit 0
What I can see from the debug output is that | is escaped with '' when the command should be run. So how can I convince the shell to interpret the pipe symbol. I also tried to escape it with \ but to no avail.

Code:
Gather all block device data (y/n): n
1) sda
2) sdb
3) sdc
Gather data of partition from (1/2/3): 1
+ echo lsblk -n -l -i -o NAME /dev/sda '|' grep -v '"sda$"'
lsblk -n -l -i -o NAME /dev/sda | grep -v "sda$"
++ lsblk -n -l -i -o NAME /dev/sda '|' grep -v '"sda$"'
lsblk: invalid option -- 'v'
Best Regards
 
Old 12-10-2013, 06:31 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
To avoid this behaviour (the pipe is interpreted literally) you can either avoid the CMD assignment and put the command directly inside the command substitution or use a shell function instead. For example this works:
Code:
for i in $(lsblk -n -l -i -o NAME /dev/$DEV | grep -v "$DEV$")
do
This works as well:
Code:
function list_partitions () {
  lsblk -n -l -i -o NAME /dev/$1 | grep -v "$1$"
}

for i in $(list_partitions $DEV)
do
Another option is the usage of eval (but it is not recommended for some reasons, as explained here). In any case this should work as well:
Code:
for i in $(eval $CMD)
do
Hope this helps.
 
Old 12-10-2013, 06:44 AM   #3
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748

Original Poster
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
I go for the evil with eval. I guess should start using functions from the beginnings and not after setting me skeleton up.

Thanks for the input. Working now.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipe a variable using grep in a script. okos Linux - Software 6 08-17-2009 02:31 AM
Find variable name by variable content using a subshell UltramaticOrange Programming 3 11-17-2008 03:51 PM
Is there a variable that represents data passed in a pipe HGeneAnthony Linux - Newbie 1 02-27-2007 11:01 PM
how to pipe/redirect awk output into a variable? johnpaulodonnell Linux - Newbie 2 01-25-2007 06:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration