How to use pipe operator as simple character in text file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use pipe operator as simple character in text file?
# 1  
Old 03-02-2013
IBM How to use pipe operator as simple character in text file?

Hello all, I have two files which are cmd and disk.
Code:
`$cat cmd  
lsdev | grep -iw`

Code:
`$cat disk  
hdisk2`

Now I want to use the contents of both the files in a way such that
Code:
`lsdev | grep -iw`

command works for hdisk2 when I write the following script:

Code:
`!#/bin/sh  
cmd1="$( sed -n '1p' cmd)"  
disk1="$ (sed -n '1p' disk)"  
comm="$cmd1 $disk1"`
echo `$comm`

Now whenever I try to run the script, an error is thrown regarding wrong usage of | in lsdev command. Please help me find a solution for the same. Thanx
# 2  
Old 03-02-2013
You have single quote within double quote, does not work. cmd1="$( sed -n '1p' cmd)"

Space after the $ gives problem disk1="$ (sed -n '1p' disk)"
This have one back ticks not two comm="$cmd1 $disk1"`

Last edited by Jotne; 03-02-2013 at 11:25 AM..
# 3  
Old 03-02-2013
Well, I will try your suggested answer in a while but have got a similar problem as well:

Code:
[root@SVHJ1315 SCRIPTING]# cat cmd
cat -n reduce_opt.sh "|" grep -iw
[root@SVHJ1315 SCRIPTING]# 
[root@SVHJ1315 SCRIPTING]# 
[root@SVHJ1315 SCRIPTING]# cat file
echo
[root@SVHJ1315 SCRIPTING]# 
[root@SVHJ1315 SCRIPTING]# cat command1 
#!/bin/sh

one=$(sed -n '1p' cmd)
two=$(sed -n '1p' file)
both=`echo $one $two`
echo `$both`
[root@SVHJ1315 SCRIPTING]# 
[root@SVHJ1315 SCRIPTING]# 
[root@SVHJ1315 SCRIPTING]# ls -lrt command1 
-rwxr-xr-x. 1 root root 94 Mar  2 20:58 command1
[root@SVHJ1315 SCRIPTING]# 
[root@SVHJ1315 SCRIPTING]# 
[root@SVHJ1315 SCRIPTING]# sh command1 
cat: invalid option -- 'i'
Try `cat --help' for more information.

[root@SVHJ1315 SCRIPTING]# 
[root@SVHJ1315 SCRIPTING]#

# 4  
Old 03-02-2013
Quote:
Originally Posted by Jotne
You have single quote within double quote, does not work. cmd1="$( sed -n '1p' cmd)"
There's nothing wrong with that syntax. Within that command substitution, the use of single or double quotes is nothing special, even if the command substitution itself is double quoted. The only effect of double-quoting an entire subsitution is that the result of the command is protected from field-splitting and filename globbing (neither of which would happen here anyway, since operations that may yield multiple fields are not performed when assigning to a variable).

From POSIX: Shell Command Language:
Quote:
With the $( command) form, all characters following the open parenthesis to the matching closing parenthesis constitute the command. Any valid shell script can be used for command, except a script consisting solely of redirections which produces unspecified results.
The same cannot be said of the older though still widely-used backtick style, ` ... `. In my opinion, that is a very persuasive reason to never use it. The backtick version can get hairy, particularly with nesting or quoting or, or (I hesitate to even contemplate the possibility) nested quoting.

Regards,
Alister

---------- Post updated at 11:49 AM ---------- Previous update was at 11:33 AM ----------

Quote:
Originally Posted by ravi.trivedi
Hello all, I have two files which are cmd and disk.
Code:
`$cat cmd  
lsdev | grep -iw`

Code:
`$cat disk  
hdisk2`

Now I want to use the contents of both the files in a way such that
Code:
`lsdev | grep -iw`

command works for hdisk2 when I write the following script:

Code:
`!#/bin/sh  
cmd1="$( sed -n '1p' cmd)"  
disk1="$ (sed -n '1p' disk)"  
comm="$cmd1 $disk1"`
echo `$comm`

Now whenever I try to run the script, an error is thrown regarding wrong usage of | in lsdev command. Please help me find a solution for the same. Thanx
The error is occurring because the pipe character in $comm is not treated specially. The shell command parsing step which interprets the pipe symbol as a special character has completed by the time the shell expands the variable containinig the pipe. For your approach to work, you'd need to use eval to pass the value of $comm (or $both, in your second example) through the shell parser. This has security implications, so if you are not familiar with eval, you should do some research.

Nothing that you've mentioned suggests you need to resort to eval. Aside from security issues, it can be a pain to protect metacharacters from the effects of going through the shell parser more than once. It would be simpler and much more secure and robust to simply parameterize your script so that the grep pattern can be a simple argument.

Regards,
Alister

Last edited by alister; 03-02-2013 at 01:02 PM..
# 5  
Old 03-02-2013
To be honest i don't understand what you intend the sed's to do. What they actually do is to reduce the file to its first line. A "head -1" would do the same, no?

Further, there is a problem when you try to expand variables which contain blanks, because blanks are special to the shell: they separate "fields", individual parts of the input line. I will show this in an example, which actually happened in a shipped version of AIX (see the full story here):

First a word of caution: create a separate directory for this to try it out, you will see why.

Suppose you want to redirect the output of a process to a file and you want to be flexible about it, so you put it into a variable: instead of

Code:
command > file

you write in your script:

Code:
destination="file"
command > "$destination"

This will work perfectly and as expected. (When you try this replace "command" with any UNIX command - "ls -l" will do, for instance.) But now suppose you want to include the error channel in your redirect too and you do it this way:

Code:
destination="file 2>&1"
command > "$destination"

What will happen? For the shell, the content of "$destination" is one word and it will redirect only the <stdout> - [i]but to a file named "file 2>&1". (This is why i told you to create a separate directory for this - you can easily remove this directory instead of having to find out how to delete files with special characters.)

How this might function? There are two possible solutions: first, create your variables so that the blanks are outside:

Code:
dest_out="file"
dest_err="&1"
command > "$dest_out" 2>"$dest_err"

Second possibility: use "eval". The reason why the shell has problems is that it parses the lines in a fixed procedure. When variables are expanded, they are all expanded together and no second pass is given to them (this is why it isn't possible to have a variable point to another variable with "normal" means). But "eval" restarts this evaluation process and this makes the shell "reconsider" the line with the now expanded blanks in it:

Code:
destination="file 2>&1"
eval command > "$destination"

Btw.: you should really REALLY stay away from the backticks. They are only supported for backwards compatibility reasons and their use is discouraged. Only use the subshell "$(...)".

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

How to run commands with pipe from text file?

Hello, I have standard loop while read -r info; do command $info done < info in info text file I have multiple commands each on line that I want to execute. When I used them in console they worked, but not with this loop. This is one of the commands in info file: grep... (4 Replies)
Discussion started by: adamlevine
4 Replies

2. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

3. Shell Programming and Scripting

Bash Script Help...search then read from file: change text and pipe back...

Hello, I am trying to make a bash script that can pull data from a file and then change one part of said data. I want to search by username and pull the full line. That way there is a way to replace just one part of that line then return it back to the file. My Data is stored like: ... (1 Reply)
Discussion started by: serverfull
1 Replies

4. UNIX for Dummies Questions & Answers

problem with pipe operator

hi i am having issues with extra pipe. i have a data file and i need to remove the extra pipe in the(example 4th and 7thline) in datafile. there are many other line and filed like this which i need to remove from files. The sample data is below: 270 31|455004|24/03/2010|0001235|72 271... (3 Replies)
Discussion started by: abhi_n123
3 Replies

5. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

6. Shell Programming and Scripting

How can I use a pipe operator in a variable: OPTION="| command"?

I have a string of commands I am piping some data through and I want to allow command line switches to select which commands are used. I want to do something like this: OPTION="| command3" command1 -a -b c.txt | command2 -d -e $OPTION >result.txt I want to do it that way because OPTION may be... (1 Reply)
Discussion started by: KenJackson
1 Replies

7. Shell Programming and Scripting

Pipe text from a file into an array

Hi Guys I have a question about filling up an array I have a file called USER_FILE.txt it contains the following: Real Name:Thomas A Username:THOMAS_A Real Name:Thomas B Username:THOMAS_B Real Name:Thomas C Username:THOMAS_C Real Name:Thomas D Username:THOMAS_D Real Name:Thomas E... (8 Replies)
Discussion started by: grahambo2005
8 Replies

8. UNIX for Dummies Questions & Answers

Replacing a field in pipe delimited TEXT File

Hi, I want to replace a field in a text delimited file with the actual number of records in the same file. HDR|ABCD|10-13-2008 to 10-19-2008.txt|10-19-2008|XYZ DTL|0|5464-1|0|02-02-2008|02-03-2008||||F||||||||| DTL|1|5464-1|1|02-02-2008|02-03-2008|1||JJJ... (3 Replies)
Discussion started by: ravi0435
3 Replies

9. UNIX for Dummies Questions & Answers

Problem working with Pipe Delimited Text file

Hello all: I have a following textfile data with name inst1.txt HDR|ABCD|10-13-2008 to 10-19-2008.txt|10-19-2008|XYZ DTL|H|5464-1|0|02-02-2008|02-03-2008||||F||||||||| DTL|D|5464-1|1|02-02-2008|02-03-2008|1||JJJ DTL|D|5464-1|2|02-02-2008|02-03-2008|1||JJJ... (9 Replies)
Discussion started by: ravi0435
9 Replies

10. UNIX Desktop Questions & Answers

Wall, Write, select users, pipe a text file, HELP Before I'm Bald!

OK... I'm fairly new to unix having the admin handed to me on a platter w/almost no training. However, being a programmer, I do pick up things fairly easily, but this one is getting the best of me. I have a unix server that runs multiple versions of the same ERP system, hand crafted for our... (1 Reply)
Discussion started by: chimodel
1 Replies
Login or Register to Ask a Question