LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 11-26-2011, 03:09 PM   #1
gearge
LQ Newbie
 
Registered: Sep 2011
Posts: 6
Blog Entries: 2

Rep: Reputation: Disabled
BASH: Assigning a string containing pipes to a variable


Hi guys,

Please consider the following sample script:

Code:
#!/bin/bash

PATTERN="rpm\|drpm\|install.img"

ls -l /var/www/centos/6/os/i386/Packages | grep -v '^total' | grep -- \
"$PATTERN" | tr -s ' ' | cut -s -d ' ' -f 5,9 >> /var/tmp/local.listing
I want to assign the following section to a variable, for later reference and/or modification:

Code:
grep -- "$PATTERN" | tr -s ' ' | cut -s -d ' ' -f 5,9
How can I do this?

Thanks.

Last edited by gearge; 11-26-2011 at 03:12 PM.
 
Old 11-26-2011, 04:48 PM   #2
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 209

Rep: Reputation: 66
alias it
Code:
alias snip='grep -- "$PATTERN" | tr -s '\'' '\'' | cut -s -d " " -f 5,9'
or make a function of it
Code:
snip() { 
        grep -- "$PATTERN" | tr -s\  | cut -s -d" " -f 5,9
}
Notice the different ways I played with your argument passing, they all work, the '\'' method is the safest thing when you want to embed single quotes in single-quoted text the shell's going to see again, because it doesn't change the way the shell treats the contents the shell's going to see on the rescan.
 
Old 11-26-2011, 05:54 PM   #3
gearge
LQ Newbie
 
Registered: Sep 2011
Posts: 6
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
Hi jthill,

Thanks for the prompt reply.

Placing the string in a function doesn't work. Based on debug output below, I will try to play a bit more with the escape character.

Quote:
root@debian-pc1:~# vi scripts/script.bsh
root@debian-pc1:~# cat scripts/script.bsh
#!/bin/bash

PATTERN="rpm\|drpm\|install.img"

function ls_fltr {
grep -- "$PATTERN" | tr -s\ | cut -s -d" " -f 5,9
}

ls -l /var/www/centos/6/os/i386/Packages | grep -v '^total' | ls_fltr >> /var/tmp/local.listing

root@debian-pc1:~# bash -x scripts/script.bsh
+ PATTERN='rpm\|drpm\|install.img'
+ grep -v '^total'
+ ls_fltr
+ ls -l /var/www/centos/6/os/i386/Packages
+ tr '-s '
+ grep -- 'rpm\|drpm\|install.img'
+ cut -s '-d ' -f 5,9
tr: invalid option -- ' '
Try `tr --help' for more information.
root@debian-pc1:~# vi scripts/script.bsh
root@debian-pc1:~# cat scripts/script.bsh
#!/bin/bash

PATTERN="rpm\|drpm\|install.img"

function ls_fltr {
grep -- "$PATTERN" | tr -s '\'' '\'' | cut -s -d " " -f 5,9
}

ls -l /var/www/centos/6/os/i386/Packages | grep -v '^total' | ls_fltr >> /var/tmp/local.listing

root@debian-pc1:~# bash -x scripts/script.bsh
+ PATTERN='rpm\|drpm\|install.img'
scripts/script.bsh: line 9: unexpected EOF while looking for matching `''
scripts/script.bsh: line 11: syntax error: unexpected end of file

Last edited by gearge; 11-26-2011 at 06:01 PM.
 
Old 11-26-2011, 06:29 PM   #4
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 209

Rep: Reputation: 66
My fault on the tr arg, put a space between the -s and the backslash. Using '\'' outside a single-quoted string fails, as you discovered.

The '\'' '\'' generates a backslash (the text between the first pair of single quotes), a space (between the second pair), a single quote (because that one is escaped by the second backslash), and the last single quote starts a string the shell runs off the end of the script looking for the end of, producing the complaints you saw.
 
Old 11-27-2011, 02:28 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Well maybe if you explained what you were trying to accomplish we could help. I say this as to process ls is a bad idea as you can read about here.
If you need an example of why this is a bad idea, simply doing an ls -l in my home directory I have no lines with 9 fields so your cut would not work as expected.
 
Old 11-27-2011, 06:03 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Quote:
Originally Posted by gearge View Post
I want to assign the following section to a variable, for later reference and/or modification:
....
How can I do this?

Thanks.
The simple answer is...you don't! Variables are designed for containing data, not code. Pipes and redirections in particular cannot be handled this way except through the use of eval, and eval has security implications.

If you want a way store a command dynamically then you should use a function. That's what they were designed for.

See here for a detailed discussion:
http://mywiki.wooledge.org/BashFAQ/050


Second, as grail pointed out, don't parse ls for filenames. Use a globbing pattern or find; and store the results in an array if you need to process the entries more than once or in an arbitrary order.

And again, if you would explain exactly what your actual goal is, and give us some examples of the input and desired output, we'll be glad to help you find a more workable solution.
 
  


Reply

Tags
bash, pipe, script, string, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Bash concatenating string to variable abercrombieande Programming 4 01-19-2011 08:04 AM
[SOLVED] BASH: if Variable -eq String not working worm5252 Programming 2 01-24-2010 04:07 PM
Assigning variables and string parsing in bash JDska55 Linux - Newbie 9 06-16-2009 11:51 AM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 11:38 AM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 09:13 AM


All times are GMT -5. The time now is 12:12 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration