read a file using pipelines
Hello,
I have written an example code for reading the content of a file maintaining the end of line format using hack from the internet. I have called the shell file "pipeTesting" and the text file to display "textExample". "pipeTesting" works if I call the file as an argument of the shell script. However, there are cases when files are retrieved via pipelines; if I provide the text to pipeTesting using a cat command, there are no arguments at all since echo $@ does not print anything. To note that I had to use -p /dev/stdin to create a case for pipeline usage and one for argument usage. Is there a way to display the content of the file maintaining the end of the line in case of a pipeline? thank you. The code looks something like this: #!/bin/bash if [ -p /dev/stdin ]; then echo $@ else while read do echo $REPLY done < $1 fi exit 0 Its application is: $ cat textExample.txt Much I marvelled this ungainly fowl to hear discourse so plainly, Though its answer little meaning- little relevancy bore; For we cannot help agreeing that no living human being Ever yet was blessed with seeing bird above his chamber door- Bird or beast upon the sculptured bust above his chamber door, With such name as "Nevermore." $ pipeTester textExample.txt Much I marvelled this ungainly fowl to hear discourse so plainly, Though its answer little meaning- little relevancy bore; For we cannot help agreeing that no living human being Ever yet was blessed with seeing bird above his chamber door- Bird or beast upon the sculptured bust above his chamber door, With such name as "Nevermore." $ cat textExample.txt | pipeTester _ |
I can tell you that you're mixing methods of input to your script.
Code:
while read Code:
./endlinescript accounts I bet this is where you got you file name an example. https://stackoverflow.com/questions/...-into-a-script I do not completely understand what you're trying to accomplish here, when their is no need to pipe in the contents of the file into a bash script in order to read same said contents. the above code does that without the pipe. as far as end of line? Code:
while read F ; |
I have never visited that page. I think there is a difference between writing scripts for arguments (that is passing arguments to the script the normal way) and one for pipeline usage since the arguments passed to the function are different. The problem is not to use one way or another, but to fix the script so that the end user can use both approaches. If I use the argument approach I properly get $1 etc, but if I use the same script in a pipeline, the order does not withstand anymore. For example, I changed the script into:
#!/bin/bash if [ -p /dev/stdin ]; then echo "input from pipeline: first argument is $1" else echo "input not from pipeline: first argument is $1" while read do echo $REPLY done < $1 fi exit 0 and the output was: $ pipeTesting textExample.txt input not from pipeline: first argument is textExample.txt Much I marvelled this ungainly fowl to hear discourse so plainly, Though its answer little meaning- little relevancy bore; For we cannot help agreeing that no living human being Ever yet was blessed with seeing bird above his chamber door- Bird or beast upon the sculptured bust above his chamber door, $ cat textExample.txt | pipeTesting input from pipeline: first argument is _ PS: _ stands for empty line |
I got that idea from you stating " using hack from the internet." and now I see your line of thought more clearly that you're wanting to write a script that can be used either way, and you want to preserve end of line on the output of the contents of the file being read?
taking from that link I gave you then modifiying it I get this which seems to be working either way Code:
#!/bin/bash Code:
cat accounts | ./endlinescript Code:
./endlinescript accounts the basic idea behind it is, if it is one thing loop it through that way, if it is a different way loop it through the other way. |
I would rather test for args, and either call a function
Code:
#!/bin/bash Code:
#!/bin/bash |
PERFECT! exactly what I was looking for. Thank you
|
All times are GMT -5. The time now is 02:51 AM. |