Run Command in Bash Script and Read Output

Different types of fustigate commands need to exist run from the terminal based on the user'due south requirements. When the user runs any command from the terminal so it shows the output if no error exists otherwise it shows the error message. Sometimes, the output of the control needs to exist stored in a variable for time to come utilize. Shell command substitution feature of bash can be used for this purpose. How you can store different types of shell commands into the variable using this feature is shown in this tutorial.

Command Commutation Syntax:

variable=$( command )
variable=$( control [pick…] argument1 arguments2 …)
variable=$( /path/to/ command )

OR

variable=` command `
variable=` control [option…] argument1 arguments2 …`
variable=`/path/to/ control `

***Notation: Don't utilize any infinite before and after the equal sign when using the to a higher place commands.

Single command output to a variable

Bash commands tin be used without any option and argument for those commands where these parts are optional. The following two examples show the uses of simple command exchange.

Instance#1:

bash `date` control is used to show the current appointment and time. The following script will shop the output of `date` command into $current_date variable by using command substitution.

$ current_date=$( engagement )
$ echo "Today is $current_date"

Output:

Case#two:

`pwd` control shows the path of the current working directory. The post-obit script stores the output of `pwd` command into the variable, $current_dir and the value of this variable is printed by using `echo` control.

$ current_dir=` pwd `
$ echo "The current directory is : $current_dir"

Output:

Control with option and argument

The option and argument are mandatory for some fustigate commands. The post-obit examples show how you lot can store the output of the command with selection and argument into a variable.

Example#three:

Bash `wc` command is used to count the total number of lines, words, and characters of whatever file. This command uses -c, -w and -l as option and filename equally the argument to generate the output. Create a text file named fruits.txt with the following data to test the side by side script.
fruits.txt

fruits.txt
Mango
Orange
Banana
Grape
Guava
Apple tree

Run the post-obit commands to count and store the total number of words in the fruits.txt file into a variable, $count_words and print the value by using `repeat` command.

$ count_words=` wc -due west fruits.txt`
$ repeat "Total words in fruits.txt is $count_words"

Output:

Instance#iv:

`cutting` is another bash command that uses option and argument to generate the output. Create a text file named weekday.txt with seven-weekday names to run the next script.

weekday.txt

Monday
Tuesday
Wednesday
Th
Friday
Saturday
Sun

Create a bash file named cmdsub1.sh with the following script.  In this script, while loop is used to read the content of weekday.txt file line by line and read the get-go three characters of each line by using `cut` control. After cutting, the string value is stored in the variable $day. Adjacent, If the argument is used to bank check the value of $day is 'Lord's day' or not. The output will print 'Sunday is the vacation' when if the condition is true otherwise it volition print the value of $twenty-four hour period.

cmdsub1.sh

#!/bin/bash
filename='weekday.txt'
while read line; practice
twenty-four hours=` repeat $line | cut -c i-three `
if [ $day == "Sun" ]
and then
repeat "Sunday is the holiday"
else
echo $day
fi
done < $filename

Run the script.

$ cat weekday.txt
$ fustigate cmdsub1.sh

Output:

Using command substitution in loop

You can store the output of command substitution into any loop variable which is shown in the side by side instance.

Case#5:

Create a file named cmdsub2.sh with the following code. Hither, `ls -d */` command is used to retrieve all directory list from the current directory. For loop is used here to read each directory from the output and shop information technology in the variable $dirname which is printed afterward.

cmdsub2.sh

#!/bin/fustigate
for dirname in $( ls -d */ )
do
echo "$dirname"
done

Run the script.

Output:

Using nested commands

How you tin can use multiple commands using pipage(|) is shown in the previous example. Just you tin can use nested commands in control substitution where the output of the outset command depends on the output of the second command and it works contrary of the pipe(|) control.

Nested control syntax:

var=`command1 \` control\``

Example#6:

Two commands, `repeat` and `who` are used in this example every bit the nested control. Here, `who` command will execute first that print the user's data of the currently logged in user. The output of the `who` command will execute by `echo` command and the output of `echo` volition store into the variable $var. Here, the output of `echo` command depends on the output of `who` control.

$ var=` echo \` who\``
$ echo $var

Output:

Using Command path

If you know the path of the command then you can run the control past specifying the command path when using control substitution. The following example shows the apply of command path.

Example#7:

`whoami` command shows the username of the currently logged in user. Past default, this command is stored in /usr/bin/ folder. Run the following script to run `whoami` control using path and shop in the variable, $output, and print the value of $output.

$ output=$( /usr/bin/ whoami )
$ echo $output

Output:

Using Command Line statement

Y'all can use the control line statement with the control as the argument in the command substitution.

Instance#8:

Create a bash file named cmdsub3.sh with the following script. `basename` command is used here to retrieve the filename from the 2nd command line argument and stored in the variable, $filename. We know the 1st command line statement is the name of the executing script which is denoted by $0.

#!/bin/fustigate
filename=` basename $1 `
repeat "The name of the file is $filename."

Run the script with the post-obit argument value.

$ bash cmdsub3.sh Desktop/temp/hullo.txt

Here, the basename of the path, Desktop/temp/hullo.txt is 'hello.txt'. So, the value of the $filename volition be hi.txt.

Output:

Conclusion:

Various uses of command substitutions are shown in this tutorial. If you need to work with multiple commands or depended commands and store the consequence temporary to do some other tasks later then you tin employ this feature in your script to go the output.

More info in the video:

Well-nigh the author

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

rossforome.blogspot.com

Source: https://linuxhint.com/bash_command_output_variable/

0 Response to "Run Command in Bash Script and Read Output"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel