【Linux】Pass arguments into a function(一)

简介: 【Linux】Pass arguments into a function

Source

Pass arguments into a function - Linux Bash Shell Scripting Tutorial Wiki (cyberciti.biz)

Let us see how to pass parameters to a Bash function.

让我们看看如何向 Bash 函数传递参数。

Passing parameters to a Bash function向 Bash 函数传递参数

  • The syntax is as follows to create user-defined functions in a shell script:

在 shell 脚本中创建用户定义函数的语法如下:


function_name(){
   command_block_here
}
## OR ##
function function_name_here(){
   command_line_block
}
## passing parameters to a Bash function ##
my_function_name(){
  arg1=$1
  arg2=$2
  command on $arg1
}

Invoke function

  • To invoke the the function use the following syntax:

要调用该函数,请使用以下语法:


my_function_name foo bar

Where,

  1. my_function_name = Your function name.
  2. foo = Argument # 1 passed to the function (positional parameter # 1).
  3. bar = Argument # 2 passed to the function.
  4. my_function_name = 您的函数名称。
  5. foo = 传递给函数的参数 # 1(位置参数 # 1)。
  1. bar = 传递给函数的参数 # 2。

Examples

Create a function called fresh.sh:

创建一个名为 fresh.sh 的函数:


#!/bin/bash
# write a function
fresh(){
   # t stores $1 argument passed to fresh()
   t=$1
   echo "fresh(): \$0 is $0"
   echo "fresh(): \$1 is $1"
   echo "fresh(): \$t is $t"
   echo "fresh(): total args passed to me $#"
   echo "fresh(): all args (\$@) passed to me -\"$@\""
   echo "fresh(): all args (\$*) passed to me -\"$*\""
}
# invoke the function with "Tomato" argument
echo "**** calling fresh() 1st time ****"
fresh Tomato
# invoke the function with total 3 arguments
echo "**** calling fresh() 2nd time ****"
fresh Tomato Onion Paneer

Save and close the file. Run it as follows:

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


chmod +x fresh.sh
./fresh.sh

Sample outputs:

样本输出:


**** calling fresh() 1st time ****
fresh(): $0 is ./fresh.sh
fresh(): $1 is Tomato
fresh(): $t is Tomato
fresh(): total args passed to me 1
fresh(): all args ($@) passed to me -"Tomato"
fresh(): all args ($*) passed to me -"Tomato"
**** calling fresh() 2nd time ****
fresh(): $0 is ./fresh.sh
fresh(): $1 is Tomato
fresh(): $t is Tomato
fresh(): total args passed to me 3
fresh(): all args ($@) passed to me -"Tomato Onion Paneer"
fresh(): all args ($*) passed to me -"Tomato Onion Paneer"

Let us try one more example. Create a new shell script to determine if given name is file or directory (cmdargs.sh):

让我们再试一个例子。创建一个新的 shell 脚本来判断给定的名称是文件还是目录(cmdargs.sh):


#!/bin/bash
# Name - cmdargs.sh
# Purpose - Passing positional parameters to user defined function 
# 目的 - 将位置参数传递给用户定义函数 
# -----------------------------------------------------------------
file="$1"
# User-defined function
is_file_dir(){
        # $f is local variable
  local f="$1"
        # file attributes comparisons using test i.e. [ ... ]
  [ -f "$f" ] && { echo "$f is a regular file."; exit 0; }
  [ -d "$f" ] && { echo "$f is a directory."; exit 0; }
  [ -L "$f" ] && { echo "$f is a symbolic link."; exit 0; }
  [ -x "$f" ] && { echo "$f is an executeble file."; exit 0; }
}
# make sure filename supplied as command line arg else die
# 确保文件名作为命令行参数提供,否则 die
[ $# -eq 0 ] && { echo "Usage: $0 filename"; exit 1; }
# invoke the is_file_dir and pass $file as arg
# 调用 is_file_dir,并将 $file 作为参数传入
is_file_dir "$file

Run it as follows:

运行方法如下


./cmdargs.sh /etc/resolv.conf
./cmdargs.sh /bin/date
./cmdargs.sh $HOME
./cmdargs.sh /sbin

Sample outputs:

样本输出:


/etc/resolv.conf is a regular file.
/bin/date is a regular file.
/home/vivek is a directory.
/sbin is a directory.

Function shell variables

How do I display function name? 如何显示功能名称?

[0](https://bash.cyberciti.biz/guide/0](https://bash.cyberciti.biz/guide/0](https://bash.cyberciti.biz/guide/0 "$0") always point to the shell script name. However, you can use an array variable called FUNCNAME which contains the names of all shell functions currently in the execution call stack. The element with index 0 is the name any currently-executing shell function.This variable exists only when a shell function is executing.

[0](https://bash.cyberciti.biz/guide/0](https://bash.cyberciti.biz/guide/0](https://bash.cyberciti.biz/guide/0 "$0")总是指向 shell 脚本名。不过,您可以使用一个名为 FUNCNAME的数组变量,它包含当前执行调用堆栈中所有 shell 函数的名称。索引为 0 的元素是当前正在执行的 shell 函数的名称。

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

相关文章
|
4月前
|
关系型数据库 MySQL Linux
【Azure 应用服务】[App Service For Linux(Function) ] Python ModuleNotFoundError: No module named 'MySQLdb'
【Azure 应用服务】[App Service For Linux(Function) ] Python ModuleNotFoundError: No module named 'MySQLdb'
|
7月前
|
Shell Linux
【Linux】Pass arguments into a function
【Linux】Pass arguments into a function
58 0
|
7月前
|
Shell Linux
【Linux】Pass arguments into a function(二)
【Linux】Pass arguments into a function
68 0
【Linux】Pass arguments into a function(二)
|
Linux Shell C语言
Linux下gcc编译时出现In function `_start‘:(.text+0x**) :解决方案
Linux下gcc编译时出现In function `_start‘:(.text+0x**) :解决方案
589 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...
2013 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...
740 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.
1313 0