Shell programming: return returns the status code of the script call

  • Requirements: How to call a script or function, and determine whether the task is successfully executed
    Insert picture description here
wang@wang-T58-V:~/sh/test-invoke-scripts$ cat slave.sh 
#!/bin/bash
#name: slave.sh
test_fun(){
	ls /var/lib/docker
	return 200 #返回方法调用的状态码
}

test_fun 
echo "test_fun -->方法调用的状态码 :$?" #获取方法执行的状态码

aaaa 
res=$?
echo "slave.sh 脚本的状态码: $res"
return $res #放回脚本执行的状态码



wang@wang-T58-V:~/sh/test-invoke-scripts$ cat master.sh 
#!/bin/bash
#name: master.sh
sh slave.sh #脚本中最后一行return 执行状态码
echo $? #获取上一个命令的状态码= 上一个脚本的return 值


wang@wang-T58-V:~/sh/test-invoke-scripts$ sh -x master.sh 
+ sh slave.sh
ls: cannot open directory '/var/lib/docker': Permission denied
test_fun -->方法调用的状态码 :200
slave.sh: 12: slave.sh: aaaa: not found
slave.sh 脚本的状态码: 127
+ echo 127
127
281 original articles published · Liked 44 · Visits 150,000+

Guess you like

Origin blog.csdn.net/eyeofeagle/article/details/105655296