如何在tcsh中检查是否存在单个标志?

问题描述 投票:0回答:2

我有一个bash脚本,它采用名为“--signal”的命令行标志。

如果在命令行中指定了这个,我想在命令提示符下运行一行。

是否有简洁的方法来做一个简短的if语句来检查? Getops增加了很多杂乱,而不是我不太喜欢。

if (--signal)
 # Run this line
endif
linux shell flags tcsh
2个回答
0
投票

蛮力但有效。用布尔值替换found的值,如果是true,则将你的进程分离以发送信号。

#!/bin/bash

found="didn't find it"
for v in "$@"
do
  if [ $v == "--signal" ]
  then
    found="found it"
  fi
done
echo $found

0
投票

问题是“tcsh”,但描述中写着“bash”。然后在评论中再次提到“tcsh”。所以,我假设你想在tcsh中使用它。

如果你不担心错误检查,比如为其他不支持的参数抛出错误等,这个简单的if语句可以满足你的需要。即检查是否将“--signal”传递给脚本

if ("$*" =~ "*--signal*") then
    # Whatever you want to do
endif
© www.soinside.com 2019 - 2024. All rights reserved.