linux - 如何从 if 条件中获取输出?

标签 linux bash if-statement centos output

不知道这个方法是否适用。

#!/bin/bash
file=$1
if [[ -x "$file" ]]
then
if [ $? = 0 ]
then 
echo $file is executable
else
echo $file is not executable
fi
fi

但是如果我使用非可执行文件运行此脚本,它不会返回任何内容 但是,如果我使用可执行文件运行此脚本,它将返回“名称可执行”

just want to know about positional parameter working. i'm a beginner.but can i take the output of the first if condition into $?

最佳答案

对于这样的测试,您不需要内部 if。此代码将完成这项工作:

#!/bin/bash
file=$1
if [[ -x "$file" ]]
 then
   echo $file is executable
 else
   echo $file is not executable
fi

关于linux - 如何从 if 条件中获取输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60595870/

相关文章:

json - 使用 jq 计算缺少键的记录

linux - awk打印一行的一些参数

linux - 递归 md5sum 在 Windows 和 Linux 上有所不同

html - 通过 HTML 对象在 Linux 上播放 *.au 文件

bash - 不能将 bash 变量用于 tar 的排除

bash - [-f : Command not found, Bash 脚本文件是否存在

PHP - 使用 if 条件重定向到不同的页面

javascript - 如果我将 Google 分析代码放在 IF 语句中,它会起作用吗?

html - 使用sed编辑html

java - Thymeleaf 中的多个 if else 条件