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
  • 4
    You need to have a space after your opening bracket and before your closing bracket in a shell test [ "$ans" = "$back" ] – Jesse_b Dec 25 '17 at 20:57
  • See Brackets in if condition: why am I getting syntax errors without whitespace? - and you may find www.shellcheck.net to be useful for issues like this – steeldriver Dec 25 '17 at 21:00
  • This may not do what you expect. It will change the directory for the shell running the script, but when the script ends and the shell exits you'll be where you started. – user3486184 Dec 25 '17 at 21:34
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
  • I apologize for the late reply but thank you for the help it very well detailed and will help me make better shell scripts. – Harikesha Suresh Dec 27 '17 at 8:56
  • I used the redone code it worked but when I selected the second option it did not go to 'git-sources' folder – Harikesha Suresh Dec 27 '17 at 9:07
  • It works for me. Post output of pwd after running the script. – Arkadiusz Drabczyk Dec 27 '17 at 9:26
  • $ pwd /home/abishtu – Harikesha Suresh Dec 27 '17 at 11:33
  • Did cd report error? Show echo $? after running the script. – Arkadiusz Drabczyk Dec 27 '17 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.