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.

 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

./shell_script  " here is the string which you need to format"

My script is not working when I run the below format.

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

There are a number of ways to do this you could default to reading input if there are not parameters like this:

$ 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
2 Likes

What you are checking with -t option

if [ -t 0] 

I am not clear with this step

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:

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

From the man test (linux) manpage:
-t FD
file descriptor FD is opened on a terminal
File descriptor 0 is stdin.

the above script is not working when we have an error.

ls x |output_log
x not found
[INFO][Dec 12 10:37:17]

when I run again

]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.

Your script is working as expected:

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.

Can I suggest redirect of stderr to stdout like this:

 ls x 2>&1 | output_log
1 Like

Basically what I am looking is to format command out put. I tried in below way also

ls -lrt 2>&1 | output_log
 [INFO][Feb 08 16:11:10]  total 112
  

I can see only first line in the output. Rest of the lines are missing.

Is it about this?

#!/usr/bin/env bash
# File: 	pipe
#
#	Variables
#
	C=0
#
#	Action & Display
#
	# Prepare arg list
	if [ $# -eq 0 ]
	then	[ -t 0 ] && printf "Input: "
		read input
	else	input=$@
	fi
	
	#Parse args
	for a in $input
	do
		echo "$C -- some text preparations $a"
		C=$(( $C + 1 ))
	done

Command / Output:

echo x y z | pipe
0 -- some text preparations x
1 -- some text preparations y
2 -- some text preparations z

As you see i've reused the template from ChubblerXL.

hth

Hi Sea,

Thanks for your reply. What I am looking is to format the out put. I want to format even when we have an error. It is working fine when we have o/p but it is failing when we have an error or output contains more than single line.

You cannot 'format' an error message.
But you can form an error message, as in catching values that would cause an error, and then format a message and print it to the user.
However, that is another step.

Could you provide some sample input, desired output and what you have tried so far?