【Linux】Pass arguments into a function(二)

简介: 【Linux】Pass arguments into a function

【Linux】Pass arguments into a function(一)https://developer.aliyun.com/article/1395339

FUNCNAME in action

Create a shell script called funcback.sh:

创建名为 funcback.sh 的 shell 脚本:


#!/bin/bash
#  funcback.sh : Use $FUNCNAME
backup(){
  local d="$1"
  [[ -z $d ]] && { echo "${FUNCNAME}(): directory name not specified"; exit 1; }
  echo "Starting backup..."
}
backup $1

Save and close the file. Run it as follows:

保存并关闭文件。按以下步骤运行:


chmod +x funcback.sh
funcback.sh /home
funcback.sh

Sample outputs:

样本输出:


backup(): directory name not specified

Return values

返回值

It is possible to pass a value from the function back to the bash using the return command. The return statement terminates the function. The syntax is as follows:

可以使用 return command将函数值传回 bash。return 语句终止函数。语法如下


return
return [value]

One can force script to exit with the return value specified by [value]. If [value] is omitted, the return status is that of the last command executed within the function or script.

可以强制脚本以 [value] 指定的返回值退出。如果省略 [value],返回状态将是函数或脚本中最后执行的命令的状态。

Examples

Create a new file called math.sh:

新建一个名为 math.sh 的文件:


#!/bin/bash
#!/bin/bash
# Name - math.sh
# Purpose - Demo return value 
# ------------------------------------
## user define function
math(){
  local a=$1
  local b=$2
  local sum=$(( a + b))
  return $sum
}
## call the math function with 5 and 10 as  arguments 
math 5 10
## display back result (returned value) using $?
echo "5 + 10 = $?"

Run it as follows:

运行方法如下


chmod +x math.sh
./math.sh

image.png

The above example is straightforward. The return command returns any given value between zero (0) and 255. By default, the value passed by the return statement is the current value of the exit status variable. For example, the following code will return wrong values as it is not between zero (0) and 255.

上面的例子很简单。return 命令 返回介于零 (0) 和 255 之间的任意给定值。默认情况下,return 语句传递的值是 exit status 变量的当前值。例如,以下代码将返回错误的值,因为它不在零(0)和 255 之间。


math 500 100

Here is the proper use of return command (bash-task.sh):

下面是 return command的正确用法(bash-task.sh):


#!/bin/bash
# Name: bash-task.sh
# Purpose: Check if a file exists or not using custom bash function and return value
# -----------------------------------------------------------------------------------
# set values 
readonly TRUE=0
readonly FALSE=1
# get input from the CLI
file="$1"
# return $TRUE (0) if file found 
# return $FALSE (1) if file not found
is_file_found(){
  [ -f "$1" ] && return $TRUE || return $FALSE
}
# show usage info if $1 not passed to our script
if [ $# -eq 0 ]
then
  echo "Usage: $0 filename"
  exit 1
fi
# let us call our function 
is_file_found "$file"
# take action based upon return value
if [ $? -eq 0 ]
then
  echo "$file added to backup task"
else
  echo "$file not found."
fi

Run it as follows:

运行方法如下


chmod +x bash-task.sh
./bash-task.sh
./bash-task.sh /etc/passwd
./bash-task.sh /foo/bar

image.png

Passing parameters to a Bash function and return true or false value 向 Bash 函数传递参数并返回 true 或 false 值

How to return custom value from a user-defined function?

As I said earlier, you can't use return command. However, the workaround is as following using the printf command/echo command:

正如前面所说,不能使用 return command。不过,变通方法如下:使用 printf 命令/echo命令


#!/bin/bash
#!/bin/bash
# Name - math.sh
# Purpose - Demo how to return custom value 
# -----------------------------------------
## user define function that use echo/printf 
math(){
  local a=$1
  local b=$2
  local sum=$(( a + b))
  echo $sum
}
## call the math function with 5 and 10 as  arguments 
total=$(math 500 42)
## display back result (returned value) using $?
echo "500 + 42 = $total"

Listing functions 列出函数

Use the typeset command or declare command.

使用 typeset commanddeclare command

Show the known functions and their code/definitions显示已知函数及其代码/定义

Open the terminal and then run:

打开终端,然后运行:


declare -f
declare -f function_name_here
## or ##
typeset -f
typeset -f function_name_here

How to remove or unset functions 如何删除或取消设置函数

Use the unset command to unset values and attributes of shell variables and functions:

使用 unset 命令 取消设置 shell 变量函数 的值和属性:


unset -f function_name_here
unset -f foo
unset -f bar
相关文章
|
5月前
|
Shell Linux
【Linux】Pass arguments into a function(一)
【Linux】Pass arguments into a function
28 0
|
3月前
|
Shell Linux
【Linux】Pass arguments into a function
【Linux】Pass arguments into a function
37 0
|
Linux Shell C语言
Linux下gcc编译时出现In function `_start‘:(.text+0x**) :解决方案
Linux下gcc编译时出现In function `_start‘:(.text+0x**) :解决方案
431 0
|
安全 Linux
linux系统下,警告:warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration] 和 warning: the `gets' function is dangerous and should
字符数组 的英文名字是 char [] gets()函数的基本用法为:char *gets(char *s); 该函数的参数是一个字符数组,该函数的返回值也是一个字符数组。 linux下的代码如下: 1 #include 2 3 int main() 4 { 5    c...
1919 0
|
测试技术 Linux
linux c in common use function reference manual
End User License Agreement   guarantees or warranties,大战前得磨刀!!!!! Tips:C Funcs Chk header Modules! Characters of the test piece(on the street sta...
707 0
|
网络协议 Linux 网络安全
Turn any Linux computer into SOCKS5 proxy in one command
src: http://www.catonmat.net/blog/linux-socks5-proxy/ I thought I'd do a shorter article on catonmat this time.
1249 0