How to pipe command output to shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pipe command output to shell script?
# 1  
Old 11-25-2014
How to pipe command output to shell script?

Hi Team,

Need a help on how to pipe a command out put to a shell script. My shell script looks like below.


Code:
 cat shell_script
 #!/usr/bin/ksh 
 input =$@
 echo "  we are inside the shell script"
 echo "  here are the input parameters"
 ..........................
 ..................
 ......................

Basically what I am trying to do is formatting the input inside the shell script.(I didn't include my complete code above)

My script is working fine when I run below format
Code:
./shell_script  " here is the string which you need to format"

My script is not working when I run the below format.
Code:
echo"here is the string which you need to format" |shell_script

It is taking null arguments when I tried to pipe the output of the command to shell script. What I want is I want my shell script to work in both ways. Similar like commands
# 2  
Old 11-25-2014
There are a number of ways to do this you could default to reading input if there are not parameters like this:

Code:
$ cat shell_script
#!/usr/bin/ksh

if [ $# -eq 0 ]
then
   if [ -t 0 ]
   then
      printf "Input: "
   fi
   read input
else
   input=$@
fi

echo "Input is: $input"
$ ./shell_script "one two" three
Input is: one two three
$ echo "piped one two" | ./shell_script
Input is: piped one two
$ ./shell_script
Input: prompted value
Input is: prompted value

These 2 Users Gave Thanks to Chubler_XL For This Post:
# 3  
Old 11-25-2014
What you are checking with -t option

Code:
if [ -t 0]

I am not clear with this step
# 4  
Old 11-25-2014
That tests if the standard input (FD # 0) is a terminal.

If it is a terminal then a prompt can be printed if it is a pipe then no prompt is necessary. The prompt should probably be printed to stderr so that if you redirect to a file it is still visible:

Code:
   if [ -t 0 ]
   then
      printf "Input: " >&2
   fi

# 5  
Old 11-25-2014
From the test manpage:
-t FD
file descriptor FD is opened on a terminal
File descriptor 0 is stdin.
# 6  
Old 12-12-2014
the above script is not working when we have an error.
Code:
ls x |output_log
x not found
[INFO][Dec 12 10:37:17]

when I run again
Code:
]ls test_file | output_log
[INFO][Dec 12 10:47:10]  test_file

---------- Post updated at 01:50 PM ---------- Previous update was at 01:49 PM ----------

This Script is not working when we have an error.
# 7  
Old 12-13-2014
Your script is working as expected:
Quote:
Originally Posted by gvkumar25
Code:
ls x |output_log
x not found
[INFO][Dec 12 10:37:17]

Stdin is not a terminal, so there is no prompt.
Nothing (ok, eof) was read from stdin, so input was set to the empty string.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get the output of w command in pipe delimited format

Since output of w command have variable number of columns I want to get the output in pipe delimited format. I tried export OFS="|"; w but that does not work. Any ideas? (4 Replies)
Discussion started by: Soham
4 Replies

2. SuSE

Find command doesn't pipe the output as required.

Hi, I am using below code snippet to echo/display the files found (matching a pattern from searchstring.out file) and the corresponding owner. while read j do echo "Pattern to search is:- $j" find / -name "*$j*" |\ while read k do echo "File found is:- $k" owner=$(ls... (9 Replies)
Discussion started by: Vipin Batra
9 Replies

3. Shell Programming and Scripting

Pipe output a command to another using xargs

xargs work great when a command gives multiple line output which can be input to another. In my case it is not working coz the second command uses two words in it. $ scr.sh gives output like 193740 638102 375449 .. .. another command takes these number as inputs. it works great... (1 Reply)
Discussion started by: mahesh113
1 Replies

4. UNIX for Dummies Questions & Answers

is there any way of using rm command on output of pipe

Hi, I am having a list of directories with different login id's. My requirement is that i need to list the directories of my id and need to delete them. So i am using following code ls -ltr ¦ grep userid ¦ rm -rf But this is not working. So is there any way of doing it. Please note... (3 Replies)
Discussion started by: sarbjit
3 Replies

5. Shell Programming and Scripting

shell script to format command output

Hello team, I am running below command which is giving following output. bash-3.00$ ps -eo pid,pcpu,args | sort +1n | grep -i java 12 0.0 grep -i java 8804 0.0 /opt/app/ccr/home/ccr/WebSphere/AppServer/java/bin/sparcv9/java -XX:+UnlockDiag 9241 0.0... (7 Replies)
Discussion started by: coolguyamy
7 Replies

6. Shell Programming and Scripting

Assign command (with pipe) output to a variable

Hi , I would like to assign command (with pipe) output to a variable. The code is as follows. The goal of the code is to get the last folder folder with a particular name pattern. myDate=`ls | grep 2009 | tail -1` echo "myDate=" $myDate However, in the presence of the pipe, the code... (3 Replies)
Discussion started by: jeff_cen
3 Replies

7. UNIX for Dummies Questions & Answers

pipe output to script as command line argument

i want to redirect output of one command as the command line argument of another script for example, say i would run this command: find . -xdev -type f -size +4096 -exec ls -al {} \; i wan to be able to do something like: echo +4096 | find . -xdev -type f -size ****** -exec... (3 Replies)
Discussion started by: IMTheNachoMan
3 Replies

8. Shell Programming and Scripting

reading command output from shell script

Hi List, How to read the output of a command executed from a script. For ex. sample_scritp.sh ------------- useradd testuser1 password testuser1 .... ..... -------------- This prompts for "password", and "confirm password", for which i need to give the values from script. Can... (4 Replies)
Discussion started by: sri b
4 Replies

9. Shell Programming and Scripting

Capturing shell script command output

I am trying to check to see if a file exists on a ftp server, well, I know that cant be done, atleast directly, So I came up with this small script ftp -n $HOST <<END_SCRIPT quote USER $USER quote PASS $PASSWD cd public_html/crap dir $FILE quit END_SCRIPT Where the $ variable... (2 Replies)
Discussion started by: designflaw
2 Replies
Login or Register to Ask a Question