What I want to achieve:
- Define a function that can be used to pipe input, such as
echo input | my_function
- This function modifies every inputted line, e.g., it adds indentation at the beginning.
- This function can be reused twice and in that case does its modification (double indent) twice, e.g.
echo input | my_function | my_function
results in\t\tinput
. - This function does not wait for whole input to be supplied, it can print out the line directly without seeing all input.
As for my test, please see the following script:
#!/usr/bin/env bash
main() {
echo 'first:'
echo 'once' | tab_indent_to_right
echo 'twice' | tab_indent_to_right | tab_indent_to_right
{
echo 'wait 2 sec'
sleep 2
echo 'wait 2 sec'
sleep 2
echo 'waited'
} | tab_indent_to_right
}
tab_indent_to_right() {
# while read -r line; do echo $'\t'"$line"; done # double indent not working
# awk -v prefix='\t' '{print prefix $0}' # buffer not working
# sed 's/^/\t/' # buffer not working
# xargs -I {} echo $'\t{}' # double indent not working
# xargs -L1 echo $'\t' # double indent not working
}
main
Each line in tab_indent_to_right
is my failed attempt to solve the problem. They have two different problems, either:
- Double indent is not working e.g.,
tab_indent_to_right | tab_indent_to_right
- Or, the lines are not being flushed/printed directly but instead being buffered. In other words, the function waits for all
sleep
s and prints out everything at once, instead of printing the lines as they come.
How can I create this function so both double calls gives me the modification I want and also the script does not wait for complete execution of the piped shell?