这个脚本有很多问题.首先,让我们检查一下
shellcheck,一个类似于lins的shell脚本工具
它:
$~/.cabal/bin/shellcheck git-installer.sh
In git-installer.sh line 17:
if [$ans == $back]; then
^-- SC1009: The mentioned parser error was in this if expression.
^-- SC1035: You need a space after the [ and before the ].
^-- SC1073: Couldn't parse this test expression.
^-- SC1020: You need a space before the ].
^-- SC1072: Missing space before ]. Fix any mentioned problems and try again.
这是你的错误的来源和建议应该做什么
避免错误.您需要在每个之前和之后添加一个空格
方括号,以便您的脚本如下所示:
#!/bin/sh
mkdir /home/my-username/git-sources
cd home/my-username/git-sources
read gitsource
git clone $gitsource
echo "Please choose from the options bellow"
echo "1)Go back to your working directory"
echo "2) GO to the 'git-sources' folder"
read ans
back="1"
stay="2"
if [ $ans == $back ]; then
cd -
elif [ $ans == $stay ]; then
cd /home/my-username/git-sources
fi
但是shellcheck仍然不满意:
$~/.cabal/bin/shellcheck git-installer.sh
In git-installer.sh line 3:
cd home/my-username/git-sources
^-- SC2164: Use cd ... || exit in case cd fails.
In git-installer.sh line 5:
read gitsource
^-- SC2162: read without -r will mangle backslashes.
In git-installer.sh line 7:
git clone $gitsource
^-- SC2086: Double quote to prevent globbing and word splitting.
In git-installer.sh line 14:
read ans
^-- SC2162: read without -r will mangle backslashes.
In git-installer.sh line 17:
if [ $ans == $back ]; then
^-- SC2086: Double quote to prevent globbing and word splitting.
^-- SC2039: In POSIX sh, == in place of = is undefined.
In git-installer.sh line 18:
cd -
^-- SC2164: Use cd ... || exit in case cd fails.
In git-installer.sh line 19:
elif [ $ans == $stay ]; then
^-- SC2086: Double quote to prevent globbing and word splitting.
^-- SC2039: In POSIX sh, == in place of = is undefined.
In git-installer.sh line 20:
cd /home/my-username/git-sources
^-- SC2164: Use cd ... || exit in case cd fails.
除了修复shellcheck报告的所有错误和警告我们
可以进一步改进这个脚本.
您可以使用简单的“$HOME”代替
/ home / my-username,以便脚本适用于每个用户.
另请注意,顶部的mkdir只有在完成时才应该完成
目录尚不存在,否则我们将得到File exists错误.
打印一个小提示给用户告诉他们会很好
我们期待他们的投入.
另请注意,为了运行cd,您可能会想象它
必须使用此脚本而不是运行它.话虽如此,
我们应该通过取消设置我们的变量来整理用户环境
为我们的需求而创造.
总而言之,它应该是:
#!/bin/sh
if [ ! -d "$HOME"/git-sources ]; then
mkdir "$HOME"/git-sources
fi
cd "$HOME"/git-sources || { printf "cd failed, exiting
" >&2; return 1; }
printf "Gitsource: "
read -r gitsource
git clone "$gitsource"
unset gitsource
echo "Please choose from the options bellow"
echo "1) Go back to your working directory"
echo "2) Go to the 'git-sources' folder"
read -r ans
back="1"
stay="2"
if [ "$ans" = "$back" ]; then
cd - || { printf "cd failed, exiting
" >&2; unset ans; return 1; }
elif [ "$ans" = "$stay" ]; then
cd "$HOME"/git-sources || { printf "cd failed, exiting
" >&2; unset ans; return 1; }
fi
unset ans
来源:
$. git-installer.sh
Gitsource: https://github.com/antirez/linenoise
Cloning into 'linenoise'...
remote: Counting objects: 396, done.
remote: Total 396 (delta 0), reused 0 (delta 0), pack-reused 396
Receiving objects: 100% (396/396), 114.69 KiB | 0 bytes/s, done.
Resolving deltas: 100% (232/232), done.
Checking connectivity... done.
Please choose from the options bellow
1) Go back to your working directory
2) Go to the 'git-sources' folder
2
$pwd
/home/ja/git-sources
$ls -Al
total 4
drwxr-xr-x 3 ja users 4096 Dec 25 22:48 linenoise