Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

linux Imagine that script1 pipes into script2, which you would write by script1

ID: 3825018 • Letter: L

Question

linux

Imagine that script1 pipes into script2, which you would write by script1 | script2. What this means is that script1 emits data (for example by echo) and that script2 is able to take that data (by read). Consider the scripts (on page2), which we'll call example1.sh, example2.sh, and example3.sh. You cannot use example3.sh in a pipe, because it's only configured to display an argument. So

echo "booh" | ./example3.sh

will display nothing, since example3.sh only displays an argument and piping passes data as input rather than argument. However, you could use example2.sh in a pipe because it displays the input accessed via the read command:

echo "booh" | ./example2.sh

would display booh. The inconvenience is that example2.sh doesn't work outside of a pipe. For example,

./example2.sh booh

would still ask you to input data, because example2.sh reads an input rather than an argument. If you want a script to which you can either give data by argument, or use in a pipe, use example1.sh. If that script has an argument then it uses it as data sources; otherwise it does a 'read', which is a way to grab data sent by pipes.

1)#!/bin/bash                              

if [ $# -eq 1 ] ;

then data=$1                                                              Can be piped or called with argument

   else

   read data

2) #!/bin/bash

   Read data                                                                    piping only

Echo “$data”

3) #!/bin/bash                                                                arguments only

    Echo”1”

Based on the explanations above, write a script which adds 1 to what is sent to it (either piped or argument). Here are two examples: should echo "5" | ./inc.sh | ./inc.sh | ./inc.sh display 8 (because it's the result of 5+1+1+1) ./inc.sh 13 (because it's 13+1)

Explanation / Answer

Hi,

Here ia your solution-

Save the below code in inc.sh file

read test

echo `expr "$test" + 1`

Save the file and run the below command

echo "5" | sh inc.sh | sh inc.sh | inc.sh

output will be 8