Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 是否可以返回在bash中动态获取的代码?_Linux_Bash_Shell - Fatal编程技术网

Linux 是否可以返回在bash中动态获取的代码?

Linux 是否可以返回在bash中动态获取的代码?,linux,bash,shell,Linux,Bash,Shell,假设我有一个bash脚本,它计算一些东西,我希望该脚本的返回代码是被计算的东西的数量。有办法做到这一点吗 我知道bash变量在技术上是无类型的,但行为“类似字符串”,并且exit不能将“字符串”作为参数 我还意识到bash返回码只能在0-255范围内,我愿意承受翻滚的后果,也就是说,计算256个东西会得到0的返回码 我想我正在寻找一个程序或其他bash内置程序,当给定一个变量时,将返回一个数值等于该变量中存储的“类似字符串”的数字的代码,这将允许我这样做: #/bin/bash $count=

假设我有一个bash脚本,它计算一些东西,我希望该脚本的返回代码是被计算的东西的数量。有办法做到这一点吗

我知道bash变量在技术上是无类型的,但行为“类似字符串”,并且
exit
不能将“字符串”作为参数

我还意识到bash返回码只能在0-255范围内,我愿意承受翻滚的后果,也就是说,计算256个东西会得到0的返回码

我想我正在寻找一个程序或其他bash内置程序,当给定一个变量时,将返回一个数值等于该变量中存储的“类似字符串”的数字的代码,这将允许我这样做:

#/bin/bash

$count=0
count_stuff    

command_to_turn_var_into_return_code $count

好的,是的-您只需编写
退出“$count”
。但在你的情况下,你不应该这样做。这与退出状态的使用方式无关,因此它会在与理解退出状态的事物的交互中产生问题

相反,您应该打印结果(使用
echo
printf
),这样就可以通过命令替换捕获结果,并通过管道传输到其他命令等

我知道[…]exit不能将“字符串”作为参数


这根本不是事实。当然,退出状态必须是一个数字,但是以字符串形式出现的数字没有问题。(Bash命令的参数总是以字符串形式出现。)

。。我发誓我已经测试过了,结果出错了。请随意嘲笑我。更详细地说:“当然,退出状态必须是一个数字,但数字以字符串形式存在没有问题”:
exit 5
exit“5”
exit“5”
相同。。。