Bash, Shell Script: How To Use Positional Parameters, Invoking A Function Part 2

bash_eyecatch Linux (Ubuntu, bash, Shell Scripts)

In part1, introduce how to pass the value of positional parameter, which has been assigned at executing the script, to invoked function.

Then let’s check how to prompt and read user input after starting executing the script and how to pass it to the function as the argument. Look at the sample script below:

#!bin/bash

### Function

list_function()
{
ls $1
}

### Main

list_function $1

echo -n “Where is target? Please type: ”

read input_argument

list_function $input_argument

exit 0

list_function $1 enables for called function to accept the value of positional parameter $1, which was assigned at executing this script. This is the topic of part1.

Then, here is the new tip.
list_function $input_argument enables for called function to accept the value of the argument input_argument that has just been gotten by read command from user input.
The reason of the name input_argument is just specifying so by read command.

If you use read target_directory,
the next line becomes list_function $target_directory to use the value.

Link to part1

タイトルとURLをコピーしました