Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 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.
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
_
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
# Check to see if a pipe exists on stdin.
if [ -p /dev/stdin ]; then
echo "Data was piped to this script!"
# If we want to read the input line by line
while IFS= read line; do
echo "Line: ${line}"
done
# Or if we want to simply grab all the data, we can simply use cat instead
# cat
else
echo "No input was found on stdin, skipping!"
# Checking to ensure a filename was specified and that it exists
if [ -f "$1" ]; then
{
while read ;
do
echo $REPLY
done <$1
}
fi
fi
piping
Code:
cat accounts | ./endlinescript
using $1
Code:
./endlinescript accounts
where the two files are in the same directory.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.