4
\$\begingroup\$

This is my very first Bash script that does something real. I'm here to learn, so please tell me if and how this can be done better.

#!/bin/sh
# Daniele Brugnara
# October - 2013
if [ -k 1ドル ]; then
 echo "Error! You must pass a repository name!"
 exit 1
fi
SVN_PATH="/var/lib/svn"
currPath=$(pwd)
cd $SVN_PATH
svnadmin create 1ドル
chown -R www-data:www-data 1ドル
cd $currPath
echo "Done! Remember to set valid user permission in authz file."
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 1, 2013 at 16:06
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$
  1. If the first argument to your script contains a space, then you get an error message:

    $ ./cr32076.sh "my repository"
    ./cr32076.sh: line 6: [: my: binary operator expected
    

    To avoid this, put quotes around the variable 1ドル, like this:

    if [ -k "1ドル" ]; then
    
  2. The man page for [ says:

    -k file True if file exists and its sticky bit is set.
    

    is this really what you mean? It seems more likely that you want the -z option:

    -z string True if the length of string is zero.
    
  3. You remember the current directory in a variable:

    currPath=$(pwd)
    

    and then later change to it:

    cd $currPath
    

    but this will go wrong if the current directory has a space in its name:

    $ pwd
    /Users/gdr/some directory
    $ currPath=$(pwd)
    $ cd $currPath
    bash: cd: /Users/gdr/some: No such file or directory
    

    To avoid this, put quotes around the variable, like this:

    cd "$currPath"
    
  4. If you want to change directory temporarily and change back again, then instead of writing

    currPath=$(pwd) # remember old directory
    cd "$SVN_PATH" # change to new directory
    # do some stuff
    cd "$currPath" # go back to old directory
    

    you can use a subshell:

    (
     cd "$SVN_PATH" # change to new directory
     # do some stuff 
    ) # old directory is automatically restored when subshell exits
    

    But in this script, there's no need to change directory back again, because after you go back all you do is echo "Done! ..." and it doesn't matter what directory you do that in.

  5. In fact, there's no need to change directory at all, because instead of

    SVN_PATH="/var/lib/svn"
    currPath=$(pwd)
    cd "$SVN_PATH"
    svnadmin create "1ドル"
    chown -R www-data:www-data "1ドル"
    cd "$currPath"
    

    you could write

    SVN_PATH="/var/lib/svn/1ドル"
    svnadmin create "$SVN_PATH"
    chown -R www-data:www-data "$SVN_PATH"
    
answered Oct 1, 2013 at 20:26
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.