零表示执行成功或非零正整数 (1-255) 表示失败。 您可以使用return语句来更改函数的退出状态。例如,查看以下linuxmi.sh脚本: #! /bin/bash//Linux迷 www.linuxmi.comerror() { blablareturn0 } errorecho"错误函数的返回状态为: $?" 如果运行linuxmi.shbash 脚本,您可能会对输出感
$./error.sh ./error.sh: line4: blabla: command not found The return status of the errorfunctionis:0 1. 2. 3. 因为有return 0这一行,所以即使函数中有 blabla 这一行的报错,error 函数还是返回了一个执行成功的状态码 0。 return 语句执行后,函数会被立即终止。 向bash 函数传递参数 我们可以向...
/bin/bash//Linux迷 www.linuxmi.comerror{blablareturn0} errorecho"错误函数的返回状态为: $?" 如果运行linuxmi.shbash 脚本,您可能会对输出感到惊讶: 如果没有return 0语句,error函数将永远不会返回非零退出状态,因为blabla会导致未找到命令错误。 正如您所看到的,即使 bash 函数不返回值,我还是通过改变函数...
#!/bin/bash function func_01() { pwd cp 000 111 } >error.txt 2>&1 function func_02() { pwd cp 000 111 } 2>&1 >>error.txt function func_03() { pwd cp 000 111 } 2>&1 >/dev/null function func_04() { pwd cp 000 111 } &> error.txt function func_05() { pwd cp 0...
我将提供一个 Bash 脚本的例子,这个脚本会检查一个特定的进程是否正在运行(例如nginx服务),如果没有运行,它就会启动这个进程。同时,这个脚本还会在一个特定的目录中查找一个特定的文件(例如在/var/log目录中查找error.log文件),并把找到的文件路径打印出来。
{ 'e', &exit_immediately_on_error}, bash-4.1\execute_cmd.c execute_command_internal (command, asynchronous, pipe_in, pipe_out, fds_to_close) if (ignore_return == 0 && invert == 0 && ((posixly_correct && interactive == 0 && special_builtin_failed) || ...
Inside the function: Declare a local variable 'num' to store the argument passed to the function. Declare another local variable 'result' and initialize it to 1. Check if 'num' is negative. If it is, we print an error message and exit the script. Next we calculate the factorial of '...
# Save the PID of the scp of a given server for laterdone# Iterate through all the servers and:# Wait for the return code of each# Check the exit code from each scpforserverin${!server_pid[*]};dowait${server_pid[$server]}test$?-ne0&&echo"ERROR: Copy from$serverhad problems, ...
注:linux中有一个经典名言【一切皆文件】,/dev/null可以认为是一个特殊的空文件,更形象点,可以理解为科幻片中的黑洞,任何信息重向定输出到它后,便有去无回,当然黑洞里也没有信息能出来。 综合来讲,上面的意思就是利用<将黑洞做为demo.txt的标准输入,黑洞里没任何内容,任何文件里的内容被它吞噬了,自然也没就...
Return 1 有错误返回 五、函数返回值测试 可以直接在脚本调用函数语句的后面使用最后状态命令来测试函数调用的返回值。例如: check_it_is_a_directory $FILENAME # this is the function call and check if [ $? == 0 ] # use the last status command now to test ...