From the course: Linux System Engineer: Bash Shell Scripting for Automation

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Pipe data into a script

Pipe data into a script

- [Instructor] A lot of commands such as less or grab can be piped to. In those cases, we can take the standard out of one command and pipe it into the standard in of those commands. We can implement this fairly easily using read. Make sure you're in your ~/bin directory. Let's create a file called readpipe.sh. Type in vi readpipe.sh, and hit Enter. Go into insert mode and add #!/bin/bash, new line, if [[ -p /dev/stdin ]]; then, new line, while IFS = read - r LINE; do, new line, echo "Line: $LINE", new line, done, new line, fi. This is a bit more complex than the other scripts, because we need to check to make sure that /dev/stdin is a pipe. If it is, then we'll read from standard in using the while read loop and assign it to the variable named Line, until we run out of data. Inside the loop, we'll display each line using echo. Save by pressing Escape, colon, W, and hitting Enter. Now let's switch to our second terminal tab, and make it executable. Type in chmod u+x readpipe.sh, now…

Contents