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
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:
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?