Question

Unix code problem Write a shell script that does the following : Read many lines of...

Unix code problem 
Write a shell script that does the following :
Read many lines of text from the standard input device (keyboard) and copy 

them to the standard output. Precede each output line with a line number

Explain all steps

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Let's break the problem into two steps:

1. Getting a multi-line input

Firstly, initialize the a variable called text to an empty string value (text = "")

Next, we read input to a variable c as "read c" for the current iteration of loop

Next, to get a multi line input, we use the while loop with the condition always set to true. This enables us to enter the string in multiple lines. Now, an infinite loop is a problem. We can choose out method of termination using the if statement:

if [ "$c" == "" ]; then
   break
fi

Here, the condition states that if the entered line is empty, the loop breaks. So, to finish your text entry, you have to press enter twice

2. Passing it to the variable so that it can be sent to the output

We now have the input. But we need to pass it to a variable in a proper format to use it effectively. Following line of code meets our requirements:

text+=$c$'\n'

It appends the line we typed in the current iteration and adds a newline character to the end of the string and appends the entire string to the "text" variable.

Once the input is finished, we have the complete string that we typed out in the variable "text". You can finally print it using echo or printf.
Note: make sure you enclose your variable within double quotes or else, your output stream ignores the newline characters and prints all the characters in a single line

Please find the complete code here:

#! /usr/bin/bash

echo "Enter Your Text here: "

text=""
while true
do
read c
  
if [ "$c" == "" ]; then
   break
fi

text+=$c$'\n'
done
echo "Your output is: "
echo "$text"

Hope this helped. :)

Add a comment
Know the answer?
Add Answer to:
Unix code problem Write a shell script that does the following : Read many lines of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SHELL SCRIPT: Provide the following questions with the codes and command line Problem 5: Take 5...

    SHELL SCRIPT: Provide the following questions with the codes and command line Problem 5: Take 5 input arguments in a for loop, but only add the even i values to sum and display the result. Problem 6: Write a shell script that accepts path of the directory and returns a count of all the files within the specified directory. (Hint: use alias to perform cd operation) Problem 7: Write a shell script that takes an ‘count’ value as an input...

  • USING Unix/Linux shell !!!: 1) Write a script (project2.sh) that will take information from a file,...

    USING Unix/Linux shell !!!: 1) Write a script (project2.sh) that will take information from a file, and print (user’s choice of printers) identical form letters to each recipient. Either E197, E-199, E-194 printers or to project2.output. Prompt the user for their choice. 2) The script and output file are due: per Moodle timeline. Please upload your fully functioning script (project2.sh) your data file (project2.input) and your output file (project2.output) into Moodle. 3) Requirements of the script. a. You can use...

  • Please help with this code For the following problem write a Matlab® script file with clear...

    Please help with this code For the following problem write a Matlab® script file with clear commentary for each line of code. Your script should: Load the data in the problem as a text file. Calculate the statistics using Matlab  functions. Solicit input from the user of the reading (for example, 137 degrees as stated in the problem). In another word, the reading should not be fixed in the script and has to be entered and saved from the command window...

  • UNIX QUESTION Write an argument-less shell script "Sum4.csh", which reads four integer values from the keyboard,...

    UNIX QUESTION Write an argument-less shell script "Sum4.csh", which reads four integer values from the keyboard, computes their sum, then displays the sum in expression format. EXAMPLE: % Sum4.csh Enter the first number: 4 Enter the second number: 7 Enter the third number: 3 Enter the fourth number: 11 RESULT: 4 + 7 + 3 + 11 = 25

  • I need this done ASAP. Please write a bash shell script that does the following: Prints...

    I need this done ASAP. Please write a bash shell script that does the following: Prints out how many users are logged on. This can be accomplished using who and wc. Prints out a list of currently logged on users. This can be accomplished using who, grep, regular expressions, and echo. Prints out how many processes you have running from past days, and a list of those jobs. Use `whoami` to get your username. Run ps -ef and see how...

  • Explain what the following UNIX command does: Is 'echo I.txt' Please type shell commands (for Windows....

    Explain what the following UNIX command does: Is 'echo I.txt' Please type shell commands (for Windows. UNIX, or any other system that you like) for: creating a directory called hello world list the contents of this directory create a file called iwashere.txt display the contents of the file iwashere.txt from the command-line Please write a simple program in C for accepting a string from the command-line, reversing and printing the reversed string to standard output.

  • WRITE A CODE IN C++ A CLASS WITH MEMBER FUCNTIONS THAT COUNTS THE LINES OF CODE...

    WRITE A CODE IN C++ A CLASS WITH MEMBER FUCNTIONS THAT COUNTS THE LINES OF CODE OF C++ FILE OR TEXT FILE .COMMENTS SHOULD NOT BE COUNTED AND SPACE BETWEEN LINES OF CODE SHOULD BE IGNORED AS WELL. ASK FOR INPUT FROM THE USER IT CAN BE CPP FILE OR TEXT FILE Assignment:Write a program to count lines of code according to your LOC Counting Standard. Notes: -Your program should accept as input the name of a file that contains...

  • Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a...

    Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns,...

  • Write a shell script called GetMyStuff.sh that will provide the above information (the script must be...

    Write a shell script called GetMyStuff.sh that will provide the above information (the script must be a executable, and must work regardless of which directory it is in). Also, just saying cat MyStuff.txt is not sufficient. (1.0%) A pipeline is the combination of at least 2 commands, where the output of command1 is the input for command2. The syntax is command1 | command2 where | is the pipeline operator. Read the man pages about the ls and wc commands. Devise...

  • The first script is validate.sh. This is a simple form validation script that will be used...

    The first script is validate.sh. This is a simple form validation script that will be used to verify the inputs given. Normally, this would be done to validate input from a website or another program before entry into a database or other record storage. In this case, we will keep it simple and only focus on the input validation step. In particular, the script should prompt the user for four values: first name, last name, zip code, and email address....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT