![用于自动克隆 git 源的 Linux Shell 脚本](https://linux22.com/image/124416/%E7%94%A8%E4%BA%8E%E8%87%AA%E5%8A%A8%E5%85%8B%E9%9A%86%20git%20%E6%BA%90%E7%9A%84%20Linux%20Shell%20%E8%84%9A%E6%9C%AC.png)
我正在尝试创建一个 shell 脚本,允许我自动克隆 git 源,代码如下所示:-
#!/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
我的问题从第 12 行开始,我想让用户可以选择是否要返回到他们的工作目录,但我在第 20 行和第 22 行收到错误,指出未找到该命令
./git-installer.sh: line 20: [1: command not found
./git-installer.sh: line 21: [1: command not found
答案1
这个脚本有很多问题。首先,让我们检查一下 类似脚本的工具shellcheck
对此有何说明:lint
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
您可能想象的方式运行,您必须使用source
此脚本而不是运行它。话虽这么说,我们应该通过取消设置我们根据需要创建的变量来整理用户环境。
总结起来应该是:
#!/bin/sh
if [ ! -d "$HOME"/git-sources ]; then
mkdir "$HOME"/git-sources
fi
cd "$HOME"/git-sources || { printf "cd failed, exiting\n" >&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\n" >&2; unset ans; return 1; }
elif [ "$ans" = "$stay" ]; then
cd "$HOME"/git-sources || { printf "cd failed, exiting\n" >&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
答案2
我遇到了同样的问题,我想克隆我的所有 GitHub 存储库(所有分支)以将其备份到文件夹中。我创建了一个 python 脚本来执行此操作,位于此处https://github.com/tisma95/github-clone如果你愿意的话你可以尝试一下。
谢谢