2

I'm trying to create a shell script that allows me to automate cloning a git source, the code is shown below:-

#!/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

My problem starts at line 12 I want make it so that the user has the option to decide if he/she wants to go back to their working directory but I get an error at line 20 and line 22 saying that the command was not found

./git-installer.sh: line 20: [1: command not found
./git-installer.sh: line 21: [1: command not found
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

2 Answers2

3

There are a lot of issues with this script. First, let's check what shellcheck, a lint-like tool for shell scripts has to say about it:

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

That's a source of your error and a suggestion what should be done to avoid the error. You need to add a whitespace before and after each square bracket so that your script looks like this:

#!/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

But shellcheck is still not happy:

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

Apart from fixing all errors and warnings reported by shellcheck we could improve this script a little further.

You can use a plain and simple "$HOME" instead of /home/my-username so that script would work for each user.

Also notice that mkdir at the top should be done only if the directory does not yet exist, we will get File exists error otherwise.

It would be nice to print a little prompt to the user telling them that we expect input from them.

Also note that in order to run cd as you probably imagine it you have to source this script instead of running it. That being said, we should unclutter user environment by unsetting the variables we created for our needs.

To sum up, it should be:

#!/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

Source it:

$ . 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
0

I had the same problem in my case I want to clone all of my GitHub repos (all branches) to backup it in folder. I created a python script to do it located here https://github.com/tisma95/github-clone you can try with it if you want.

Thanks

  • 1
    You should put the solution on SE and not link to GitHub, this is noted in the SE notes... if github decided to move your project/link, then it is broken (or if you inadvertently remove it). That is why we like SE to link to SE only. – number9 Jul 16 '23 at 23:07