Files
knowledge-kit/assets/xcode_run_cmd.sh
2025-06-23 01:18:55 +08:00

56 lines
2.1 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
ExecuteCommand() {
#判断全局字符串VERBOSE_SCRIPT_LOGGING是否为空。-n string判断字符串是否非空
#[[是 bash 程序语言的关键字。用于判断
if [[ -n "$VERBOSE_SCRIPT_LOGGING" ]]; then
#作为一个字符串输出所有参数。使用时加引号"$*" 会将所有的参数作为一个整体,以"$1 $2 … $n"的形式输出所有参数
if [[ -n "$TTY" ]]; then
echo "$@" 1>$TTY
else
echo "$*"
fi
echo "------------------------------------------------------------------------------" 1>$TTY
fi
#与$*相同。但是使用时加引号,并在引号中返回每个参数。"$@" 会将各个参数分开,以"$1" "$2" … "$n" 的形式输出所有参数
if [[ -n "$TTY" ]]; then
echo `$@ &>$TTY`
else
"$@"
fi
#显示最后命令的退出状态。0表示没有错误其他任何值表明有错误。
return $?
}
DisplayError() {
#在shell脚本中默认情况下总是有三个文件处于打开状态标准输入(键盘输入)、标准输出输出到屏幕、标准错误也是输出到屏幕它们分别对应的文件描述符是012
# > 默认为标准输出重定向,与 1> 相同
# 2>&1 意思是把 标准错误输出 重定向到 标准输出.
# &>file 意思是把标准输出 和 标准错误输出 都重定向到文件file中
# 1>&2 将标准输出重定向到标准错误输出。实际上就是打印所有参数已标准错误格式
if [[ -n "$TTY" ]]; then
echo "$@" 1>&2>$TTY
else
echo "$@" 1>&2
fi
}
ExecuteCMDAndDisplayInTerminal() {
if [[ ! -e "$TTY" ]]; then
DisplayError "=========================================="
DisplayError "❌ ERROR Occured: Did not config tty to output."
exit -1
fi
if [[ -n "$CMD" ]]; then
ExecuteCommand "$CMD" ${CMD_FLAG}
else
DisplayError "=========================================="
DisplayError "❌ ERROR: Failed to run CMD. THE CMD must not be null"
fi
}
ExecuteCMDAndDisplayInTerminal