Gitソースを自動的に複製するためのLinuxシェルスクリプト

Gitソースを自動的に複製するためのLinuxシェルスクリプト

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、スクリプトに似たツールがこれについて何を言っているのかを見てみましょう。lintshell

$ ~/.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

おすすめ記事