How to run commands with pipe from text file?


 
Thread Tools Search this Thread
Operating Systems Linux How to run commands with pipe from text file?
# 1  
Old 03-11-2018
How to run commands with pipe from text file?

Hello,

I have standard loop

Code:
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:
Code:
grep --line-buffered "title" dir/file.html | grep -Po '(?<=>)(.*)(?=<)'

It works with first grep, but not with second one. I found --line-buffered parameter, but didn't helped. Can somebody help me?
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 03-11-2018 at 10:37 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 03-11-2018
Quote:
Originally Posted by adamlevine
Hello,

I have standard loop

Code:
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:
Code:
grep --line-buffered "title" dir/file.html | grep -Po '(?<=>)(.*)(?=<)'

It works with first grep, but not with second one. I found --line-buffered parameter, but didn't helped. Can somebody help me?
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.
Please always include the operating system and shell you're using whenever you start a thread in this forum. Some utilities are not available on all operating systems. Some options on common utilities vary from operating system to operating system. Some shell options (such as arrays) vary from shell to shell. If we don't know what operating system and shell you're using, it is sometimes hard to guess at what the real problem is that you're describing, and hard to create suggestions that will work in your environment.

The command utility takes a "simple" command (a utility and its arguments) as parameters. If does not take a "pipeline", a "compound" command, a "list", an if, a for, or an until loop, or a case statement.

If you want your shell to execute a pipeline (or one of the other non-simple commands listed above) and return the exit status of that pipeline, you need something more like:
Code:
shell -c "pipeline"

where shell is the name of the shell you're using and pipeline is the pipeline you want your shell to run.
# 3  
Old 03-12-2018
Your snippet seems to want to execute every single line in the info file - why not source your file immediately, or just run it (after having chmoded it executable)?
# 4  
Old 05-16-2018
oh....
# 5  
Old 05-20-2018
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ksh: run commands from pipe

I create commands within a pipe and finally want them to be executed instead of being displayed on the screen. What is the last stage in this pipe? I found by guessing that "ksh" is working, but is this the best to use here? It boils down to this: echo "print Hello World!"| kshWhat is the... (15 Replies)
Discussion started by: Cochise
15 Replies

2. Shell Programming and Scripting

Perl telnet to router run commands from file

I have a perl script that is called with a router name command list file and output file. The command file can be very large, up to 3k of commands. At first I dumped the command list file to an array and ran the entire file on the router and captured the output. It worked ok for a command list in... (1 Reply)
Discussion started by: numele
1 Replies

3. Shell Programming and Scripting

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

Hello all, I have two files which are cmd and disk. `$cat cmd lsdev | grep -iw` `$cat disk hdisk2` Now I want to use the contents of both the files in a way such that `lsdev | grep -iw` command works for hdisk2 when I write the following script: `!#/bin/sh cmd1="$( sed -n... (4 Replies)
Discussion started by: ravi.trivedi
4 Replies

4. Shell Programming and Scripting

Help with shell script to run the commands reading options from local file

I have to use shell script to run series of commands on another unix box by connecting through SSH and giving user credentials. For running commands on remote machine I have to use options reading from a local file. Process: Connecting to remote unix server <host1.ibm.com> through ssh Login: ... (2 Replies)
Discussion started by: itsprout
2 Replies

5. UNIX for Dummies Questions & Answers

check for a file and run some commands

Hi all, Can you guys please help me with this... I am on AIX and need to prepare a script which will 1. check for a file named do_backup in the current file system 2. If the file exists i need to run some commands and exit, if the file doesn't exist then sleep for 15 mins and try... (2 Replies)
Discussion started by: family_guy
2 Replies

6. UNIX for Dummies Questions & Answers

Command to run multiple commands from a file.

I need a command, which could run mutliple commands from a file. Let's say, I have mv fileA1 fileB1 mv fileA2 fileB2 ..... mv fileA20 fileB20 I put these commands in a file, then I need a command to run the file as a whole so that I don't need to type 20 times... Anyone tell me how to... (8 Replies)
Discussion started by: kaixinsjtu
8 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