Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe output to bash function

Tags:

linux

bash

pipe

I have as simple function in a bash script and I would like to pipe stdout to it as an input.

jc_hms(){   printf "$1" } 

I'd like to use it in this manner.

var=`echo "teststring" | jc_hms` 

Of course I used redundant functions echo and printf to simplify the question, but you get the idea. Right now I get a "not found" error, which I assume means my parameter delimiting is wrong (the "$1" part). Any suggestions?

Originally the jc_hms function was used like this:

echo `jc_hms "teststring"` > //dev/tts/0 

but I'd like to store the results in a variable for further processing first, before sending it to the serial port.

EDIT: So to clarify, I am NOT trying to print stuff to the serial port, I'd like to interface to my bash functions should the "|" pipe character, and I am wondering if this is possible.

EDIT: Alright, here's the full function.

jc_hms(){   hr=$(($1 / 3600))   min=$(($1 / 60))   sec=$(($1 % 60))    printf "$hs:%02d:%02d" $min $sec } 

I'm using the function to form a string which come this line of code

songplaytime=`echo $songtime | awk '{print S1 }'` printstring="`jc_hms $songplaytime`"  #store resulting string in printstring 

Where $songtime is a string expressed as "playtime totaltime" delimited by a space.

I wish I can just do this in one line, and pipe it after the awk

printstring=`echo $songtime | awk '{print S1 }' | jc_hms` 

like so.

like image 732
jliu83 Avatar asked Jul 12 '12 14:07

jliu83


People also ask

Can you use pipe in shell script?

The pipe character | is used to connect the output from one command to the input of another. > is used to redirect standard output to a file. Try it in the shell-lesson-data/exercise-data/proteins directory!

What does $() mean in bash?

Dollar sign $ (Variable) The dollar sign before the thing in parenthesis usually refers to a variable. This means that this command is either passing an argument to that variable from a bash script or is getting the value of that variable for something.

What does || mean in shell script?

Logical OR Operator ( || ) in Bash The logical OR operator || processes multiple values. It is usually used with boolean values and returns a boolean value. It returns true if at least one of the operands is true. Returns false if all values are false.

What does #$ mean in bash?

#$ does "nothing", as # is starting comment and everything behind it on the same line is ignored (with the notable exception of the "shebang"). $# prints the number of arguments passed to a shell script (like $* prints all arguments). Follow this answer to receive notifications. edited Jul 9 at 13:55.


1 Answers

To answer your actual question, when a shell function is on the receiving end of a pipe, standard input is inherited by all commands in the function, but only commands that actually read form their standard input consume any data. For commands that run one after the other, later commands can only see what isn't consumed by previous commands. When two commands run in parallel, which commands see which data depends on how the OS schedules the commands.

Since printf is the first and only command in your function, standard input is effectively ignored. There are several ways around that, including using the read built-in to read standard input into a variable which can be passed to printf:

jc_hms () {     read foo     hr=$(($foo / 3600))     min=$(($foo / 60))     sec=$(($foo % 60))     printf "%d:%02d:%02d" "$hr" "$min" "$sec" } 

However, since your need for a pipeline seems to depend on your perceived need to use awk, let me suggest the following alternative:

printstring=$( jc_hms $songtime ) 

Since songtime consists of a space-separated pair of numbers, the shell performs word-splitting on the value of songtime, and jc_hms sees two separate parameters. This requires no change in the definition of jc_hms, and no need to pipe anything into it via standard input.

If you still have a different reason for jc_hms to read standard input, please let us know.

like image 57
chepner Avatar answered Nov 03 '22 00:11

chepner