In User input section we would learn about, how can we take in our scripts.
Sometimes while making scripts, we are not sure about what changes we want to make in our system, so we keep some option in our scripts, so that we can choose on run.
The syntax for taking a user input in bash shell is very easy we just need to write ‘read’ before the variable name.
Here is the example for a code in which we used user inputs:
#! /bin/bash
echo "Whats your name?"
read name
echo "The name of our academy is $name"
Output: -
In bash scripts we can also take inputs without even declaring any variable. We just need to write read in our code and output we can use ‘$REPLY’ in our code.
Here is the example:
#! /bin/bash
# in this file we will create a script without using a variable take user inputs
echo "Whats your name?"
read
echo "The name is $REPLY"
echo "Whats your age?"
read
echo "The person is $REPLY years old."
Output:
In bash we can also make our variable’s inputs silent so that no one can see what we have given as an input. This used usually while taking passwords in Linux. You have seen that whenever we type any password you can type but you won’t see anything, but while typing username you can see it clearly.
So, to do that you just need to put some tags before the variable -p and -sp.
Here is the example for the hidden user inputs.
#!/bin/bash
read -p "user_name : " user_var
read -sp "password : " pass_var
echo
echo "user_name : " $user_var
echo "password : " $pass_var
In bash shell we can also take various inputs using a array, and print them.
To take various inputs at once we just need to add -a before the variable and you will be able to take multiple inputs in a row.
Example:
#!/bin/bash
# Reading multiple inputs using an array
echo "Enter the inputs : "
read -a numbers
echo "The entered names are : ${numbers[0]}, ${numbers[1]} , ${numbers[2]}, ${numbers[3]}, ${numbers[4]}"
Output:
Bash date
Bash Date Formatting
In this chapter/topic, we'll learn accessible format possibilities for date instruction and know-how can we utilize them with Bash Scripts.
Bash Date
Bash shell provides different date commands alongside different formatting options. we will utilize these instructions to format Bash date to a needed one.
Bash Date Command
We can use the `date` command to display or change the present date and value of the system. we will print date and time values in several formats by using the date command. we will also use this command for calculating date and time value-related tasks. If the `date` command is employed with none option, then it'll print the present system's date and value. This command carries numerous formatting alternatives to format the output.
$ date
We can easily format date, we have various options in bash. We will use spaces with format option.
Here is the syntax to format the date.
$ date '+<format-option-codes><format-option-codes> <format-option-codes>'
As we know we can use various formats in our code, here is the list of date format options.
Bash Sleep
In this topic, you'll understand the way to use the sleep command by using different bash scripts. Sleep can be defined as a command-line advantage that authorises us to refuse the calling method for a designated time. In other words, the Bash sleep command is employed to insert a delay or pause the execution for a specified period of your time.
When the programmer must pause the execution of any command for a selected purpose, then this command are often used with the precise value . One can set the delay amount by seconds (s), minutes (m), hours (h), and days (d). This command is particularly useful when it's used within a bash shell script.
Sleep Command Syntax
Syntax: - sleep number[suffix]
We can use 4 different suffixes to a sleep command, though suffix is an optional part. Adding a suffix is on us, if we want to then we can add or else or we can leave with just with numbers. Number could be a integer or a fractional number it’s on us.
- s- Seconds
- m – Minute
- h- hours
- d- days.
Some how if we forget to add the suffix the number is in seconds by default. So we don’t need to be worried while using the sleep command in our scripts.
Example:
#!/bin/bash
# lets begin the task
date +"%H:%M:%S"
echo " you have to wait for 10 seconds"
# sleep for 9 seconds
sleep 10s
# you can also use "sleep 9" in place of "sleep 9s" because if there is no suffix, it is considered as "seconds".
# end time
date +"%H:%M:%S"
echo "Your Task just got Completed"
Output:
How the script works
When we run the script, it'll print the present time in HH:MM: SS format. Then the echo instruction will run and after execution, it will display the message "wait for 10 seconds". Then the sleep command will execute and pause the script for 10 seconds. When the required period of time elapses, the subsequent line of the script will again print the present time. Lastly, the echo command will print the message "Your Task Just got Completed".
Similarly, you'll run the sleep command for minutes, hours, and days.