Input command in linux

Bash Scripting Tutorial — 3. User Input

We looked at one form of user input (command line arguments) in the previous section. Now we would like to introduce other ways the user may provide input to the Bash script. Following this we’ll have a discussion on when and where is best to use each method.

After the mammoth previous section this one is much easier to get through.

Ask the User for Input

If we would like to ask the user for input then we use a command called read. This command takes the input and will save it into a variable.

Let’s look at a simple example:

introduction.sh

  1. #!/bin/bash
  2. # Ask the user for their name
  3. echo Hello, who am I talking to?
  4. read varname
  5. echo It\’s nice to meet you $varname
  • Line 4 — Print a message asking the user for input.
  • Line 6 — Run the command read and save the users response into the variable varname
  • Line 8echo another message just to verify the read command worked. Note: I had to put a backslash ( \ ) in front of the ‘ so that it was escaped.
  1. ./introduction.sh
  2. Hello, who am I talking to?
  3. Ryan
  4. It’s nice to meet you Ryan
  • Note: Ryan above is in italics just to show that it was something I typed in. On your terminal input will show up normally.

More with Read

You are able to alter the behaviour of read with a variety of command line options. (See the man page for read to see all of them.) Two commonly used options however are -p which allows you to specify a prompt and -s which makes the input silent. This can make it easy to ask for a username and password combination like the example below:

login.sh

  1. #!/bin/bash
  2. # Ask the user for login details
  3. read -p ‘Username: ‘ uservar
  4. read -sp ‘Password: ‘ passvar
  5. echo
  6. echo Thankyou $uservar we now have your login details
  • On lines 4 and 5 above we include the prompt within quotes so we can have a space included with it. Otherwise the user input will start straight after the last character of the prompt which isn’t ideal from a readability point of view.
  1. ./login.sh
  2. Username: ryan
  3. Password:
  4. Thankyou ryan we now have your login details

More variables

So far we have looked at a single word as input. We can do more than that however.

cars.sh

  1. #!/bin/bash
  2. # Demonstrate how read actually works
  3. echo What cars do you like?
  4. read car1 car2 car3
  5. echo Your first car was: $car1
  6. echo Your second car was: $car2
  7. echo Your third car was: $car3
  1. ./cars.sh
  2. What cars do you like?
  3. Jaguar Maserati Bentley
  4. Your first car was: Jaguar
  5. Your second car was: Maserati
  6. Your third car was: Bentley
  7. ./cars.sh
  8. What cars do you like?
  9. Jaguar Maserati Bentley Lotus
  10. Your first car was: Jaguar
  11. Your second car was: Maserati
  12. Your third car was: Bentley Lotus
Читайте также:  Комбинации клавиш linux debian

The general mechanism is that you can supply several variable names to read. Read will then take your input and split it on whitespace. The first item will then be assigned to the first variable name, the second item to the second variable name and so on. If there are more items than variable names then the remaining items will all be added to the last variable name. If there are less items than variable names then the remaining variable names will be set to blank or null.

Reading from STDIN

It’s common in Linux to pipe a series of simple, single purpose commands together to create a larger solution tailored to our exact needs. The ability to do this is one of the real strenghs of Linux. It turns out that we can easily accommodate this mechanism with our scripts also. By doing so we can create scripts that act as filters to modify data in specific ways for us.

Bash accomodates piping and redirection by way of special files. Each process gets it’s own set of files (one for STDIN, STDOUT and STDERR respectively) and they are linked when piping or redirection is invoked. Each process gets the following files:

To make life more convenient the system creates some shortcuts for us:

  • STDIN — /dev/stdin or /proc/self/fd/0
  • STDOUT — /dev/stdout or /proc/self/fd/1
  • STDERR — /dev/stderr or /proc/self/fd/2

fd in the paths above stands for file descriptor.

So if we would like to make our script able to process data that is piped to it all we need to do is read the relevant file. All of the files mentioned above behave like normal files.

summary

  1. #!/bin/bash
  2. # A basic summary of my sales report
  3. echo Here is a summary of the sales data:
  4. echo ====================================
  5. echo
  6. cat /dev/stdin | cut -d’ ‘ -f 2,3 | sort
  • Lines 4, 5, 6 — Print a title for the output
  • Line 8cat the file representing STDIN, cut setting the delimiter to a space, fields 2 and 3 then sort the output.
  1. cat salesdata.txt
  2. Fred apples 20 July 4
  3. Susy oranges 5 July 7
  4. Mark watermelons 12 July 10
  5. Terry peaches 7 July 15
  6. cat salesdata.txt | ./summary
  7. Here is a summary of the sales data:
  8. ====================================
  9. apples 20
  10. oranges 5
  11. peaches 7
  12. watermelons 12

So which should I use?

So we now have 3 methods for getting input from the user:

  • Command line arguments
  • Read input during script execution
  • Accept data that has been redirected into the Bash script via STDIN

Which method is best depends on the situation.

You should normally favor command line arguments wherever possible. They are the most convenient for users as the data will be stored in their command history so they can easily return to it. It is also the best approach if your script may be called by other scripts or processes (eg. maybe you want it to run periodically using CRON).

Sometimes the nature of the data is such that it would not be ideal for it to be stored in peoples command histories etc. A good example of this is login credentials (username and password). In these circumstances it is best to read the data during script execution.

Читайте также:  Рабочие области linux mint

If all the script is doing is processing data in a certain way then it is probably best to work with STDIN. This way it can easily be added into a pipeline.

Sometimes you may find that a combination is ideal. The user may supply a filename as a command line argument and if not then the script will process what it finds on STDIN (when we look at If statements we’ll see how this may be achieved). Or maybe command line arguments define certain behaviour but read is also used to ask for more information if required.

Ultimatately you should think about 3 factors when deciding how users will supply data to your Bash script:

  • Ease of use — which of these methods will make it easiest for users to use my script?
  • Security — Is there sensitive data which I should handle appropriately?
  • Robustness — Can I make it so that my scripts operation is intuitive and flexible and also make it harder to make simple mistakes?

Summary

read varName Read input from the user and store it in the variable varName. /dev/stdin A file you can read to get the STDIN for the Bash script

Activities

  • Create a simple script which will ask the user for a few pieces of information then combine this into a message which is echo’d to the screen.
  • Add to the previous script to add in some data coming from command line arguments and maybe some of the other system variables.
  • Create a script which will take data from STDIN and print the 3rd line only.
  • Now play about with creating a script which will behave as a filter. Create a script which will rearrange the output of the command ls -l in a useful way (eg maybe you only print the filename, size and owner) (Hint: awk can be useful here).

Источник

Prompt for Input in Bash

The read command is used to take an input from the user in the Bash script. If no variable is used after this command, the $REPLY variable is used to read the input value. The “read” command has many options to take an input which are explained in another tutorial. The use of the prompt option is one of them. This option is used to take an input from the user using a prompt message that helps the user to understand what type of data needs to be provided. The methods of using the prompt option to take an input from the user in Bash are explained in this tutorial.

Different Examples of Prompt Input

The different uses of the “read” command with the prompt message are shown in this part of the tutorial.

Example 1: Take the String Input Using Prompt

Create a Bash file with the following script that takes the first input without the prompt message into the default variable which is $REPLY, the second input without the prompt message into the $lang variable, and the third input with the prompt message into the $answer variable. Next, the messages are printed based on the input values. If the value of the $REPLY variable is “y”, “Y”, or “Yes”, the “You like programming” message is printed. If the value of the $lang variable is non-empty, the value of $lang is printed. If the value of the $answer variable is “y”, “Y”, or “Yes”, the “Your favorite language is” message is printed with the $answer value.

Читайте также:  Сколько весит linux ubuntu

echo «Do you like programming? (y/n)»

#Read input in the default variable

echo «What is your favorite programming language?»

#Read input in the assigned variable

#Read input using a prompt message

read -p «Do you like Bash Programming?(Yes/No)» answer

echo «***Output based on the input values:***»

#Output based on the first input

if [ [ $REPLY == «y» || $REPLY == «Y» || $REPLY == «Yes» ] ] ; then

echo «You like programming.»

#Output based on the second input

echo «Your favorite language is $lang «

#Output based on the third input

if [ [ $answer == «y» || $answer == «Y» || $answer == «Yes» ] ] ; then

echo «You like bash programming.»

The following output appears after executing the script with the first input value of “y”, the second input value of “PHP”, and the third input value of “y”:

Example 2: Take the Numeric Input Using Prompt

Create a Bash file with the following script that takes a numeric value from the user using a prompt message into the $number variable. Next, the value is compared with the particular numbers to find if the number matches with any ticket or not, and prints a message based on the return value of the “if” statement.

read -p «Enter your ticket number 1219:» number

#Print output based on the input

if [ [ $number -eq «7823» ] ] ; then

echo «You won the first prize.»

elif [ [ $number -eq «3489» ] ] ; then

echo «You won the second prize.»

elif [ [ $number -eq «5634» ] ] ; then

echo «You won the third prize.»

The script is executed two times. The “Try again” message is printed when the 6734 number is taken as the input value. The “You won the second prize” message is printed when the 3489 number is taken as the input value because it matches with the second “if” condition.

p2

Example 3: Take Multiple Input Values Using Prompt

In the previous two examples, the single input is taken using each “read” command. But you can take multiple inputs in Bash using the “read” command. In this case, you have to pass the multiple variables with a space when taking multiple inputs using the “read” command. Create a Bash file with the following script that takes three inputs into three variables – $id, $batch, and $cgpa. The values of these variables are printed later.

echo «=====Enter student information with space=====»

#Take multiple input values using a prompt message

read -p «ID, Batch, CGPA: » id batch cgpa

echo «***Output based on the input values:***»

The following output appears after executing the script with the values of batch=42, and cgpa=3.97:

Conclusion

The uses of the prompt with the “read” command in the Bash script are shown in this tutorial using multiple examples. The benefits of using a prompt message with the “read” command is cleared after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

Оцените статью
Adblock
detector