gpt4 book ai didi

bash - 如果 grep 找到它要查找的内容,则执行 X else Y

转载 作者:行者123 更新时间:2023-11-29 08:44:51 25 4
gpt4 key购买 nike

<分区>

在这条语句中,我试图匹配路径/app/$var1(应用程序名称)中是否存在版本($var2)

if 
find /app/$var1 -maxdepth 1 -type l -o -type d | grep $var2 #results in a nice list where i can manually get a true match.
# if match is found then execute command.
$realcmd "$@"
rc=$?
exit $rc
else
echo "no match, the version you are looking for does not exist"
fi

当前代码:这包括我所有的代码(未清理)。我运行的命令:“./xmodule load firefox/3.6.12”这个版本确实退出了

#!/bin/bash
# hook for some commands

#echo $@ #value of sting that is entered after "xmodule"

cmd=$(basename "$0")
#echo "called as $cmd"

if [[ $cmd = "xmodule" ]]
then
realcmd='/app/modules/0/bin/modulecmd tcsh'

# verify parameters
fi
# check if $@ contains value "/" to determine if a specific version is requested.
case "$@" in
*/*)
echo "has slash"
var1=$(echo "$@" | grep -Eio '\s\w*') # Gets the aplication name and put it into var1
echo $var1 # is not printed should be "firefox"
var2=$(echo "$@" | grep -o '[^/]*$') # Gets version name and put it into var2
echo $var2
# Checking if version number exist in /app/appname/
if find /app/$var1 -noleaf -maxdepth 1 -type l -o -type d | grep $var2; then
$realcmd "$@"
exit $?
else
echo "no match, the version you are looking for does not exist"
# Should there be an exit here?
fi
;;

*)
echo "doesn't have a slash"
;;
esac

输出:mycomputer [9:55am] [user/Desktop/script] -> ./xmodule load firefox/3.6.12'有斜杠

3.6.12不匹配,您要查找的版本不存在

有空格的地方(3.6.1以上)应该是应用名称。我现在意识到这一定是我的问题,因为它使用的路径可能只是/app。但我认为我没有更改那部分代码中的任何内容。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com