Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

The second issue I see is that you don't check for error conditions. A relatively easy handler is to add the exit-on-error flag to your shell script. Consider adding set -e at the beginning of the script (or a more elaborate system, but your script should be fine with set -e. For more information see: What does set -e mean in a bash script? What does set -e mean in a bash script?

The second issue I see is that you don't check for error conditions. A relatively easy handler is to add the exit-on-error flag to your shell script. Consider adding set -e at the beginning of the script (or a more elaborate system, but your script should be fine with set -e. For more information see: What does set -e mean in a bash script?

The second issue I see is that you don't check for error conditions. A relatively easy handler is to add the exit-on-error flag to your shell script. Consider adding set -e at the beginning of the script (or a more elaborate system, but your script should be fine with set -e. For more information see: What does set -e mean in a bash script?

error code is not standardized....
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419

Another little issue is that you should exit with a non-zero if the parameters are wrong (2 is a common exit code for bad parametersuse exit 1 instead of exit)...

echo -e "mkve.sh - make virtual environment\nUsage: mkve.bash project_name user_name" 
exit 21 

Another little issue is that you should exit with a non-zero if the parameters are wrong (2 is a common exit code for bad parameters)...

echo -e "mkve.sh - make virtual environment\nUsage: mkve.bash project_name user_name" 
exit 2 

Another little issue is that you should exit with a non-zero if the parameters are wrong (use exit 1 instead of exit)...

echo -e "mkve.sh - make virtual environment\nUsage: mkve.bash project_name user_name" 
exit 1 
examples
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419

Another little issue is that you should exit with a non-zero if the parameters are wrong (2 is a common exit code for bad parameters)...

echo -e "mkve.sh - make virtual environment\nUsage: mkve.bash project_name user_name" 
exit

should be:

echo -e "mkve.sh - make virtual environment\nUsage: mkve.bash project_name user_name" 
exit 2 

Yet another small issue is that it's normal to not have .sh as the extension for exectuable shell scripts. Why call your script mkve.sh instead of mkve? Also, you call it two different things in the error message, mkve.sh and mkve.bash.... which is right?

Finally, it is unconventional to put home directories in the /mnt folder. That folder has a special purpose when mounting file systems that are not normally mounted (things like CD's, USB drives, etc.). Using that folder for these home directories will probably lead to unexpected issues if/when someone mounts a drove over /mnt and all your home folders disappear.

The end code would be something like (untested):

#! /bin/bash 
# exit if there's a failed command
set -e
if [ $# -lt 2 ] 
 then 
 echo -e "mkve - make virtual environment\nUsage: mkve project_name user_name" 
 exit 2
fi
pname=1ドル
puser=2ドル
echo project name: $pname
echo project user: $puser
# create the webapp user and its home directory, and give it ownership 
sudo useradd --system --gid webapps --shell /bin/bash --home /ves/$pname $puser 
sudo mkdir -p /mnt/u1/$pname
sudo chown $puser /mnt/u1/$pname
# become the webapp user, create a virtual environment, start it, and install gunicorn and flask 
sudo --user $puser bash -c "cd ~; virtualenv ./venv" 
sudo --user $puser bash -c "cd ~;. venv/bin/activate; pip install gunicorn flask"
echo -e "Done! Start with:\n$ sudo -u $puser -i\n$ . venv/bin/activate" 
echo -e "Or undo the whole kaboodle with:\n$ sudo userdel -r $puser" 
# at this point, manual configuration is necessary, which is different for each project. 

Finally, it is unconventional to put home directories in the /mnt folder. That folder has a special purpose when mounting file systems that are not normally mounted (things like CD's, USB drives, etc.). Using that folder for these home directories will probably lead to unexpected issues if/when someone mounts a drove over /mnt and all your home folders disappear.

Another little issue is that you should exit with a non-zero if the parameters are wrong (2 is a common exit code for bad parameters)...

echo -e "mkve.sh - make virtual environment\nUsage: mkve.bash project_name user_name" 
exit

should be:

echo -e "mkve.sh - make virtual environment\nUsage: mkve.bash project_name user_name" 
exit 2 

Yet another small issue is that it's normal to not have .sh as the extension for exectuable shell scripts. Why call your script mkve.sh instead of mkve? Also, you call it two different things in the error message, mkve.sh and mkve.bash.... which is right?

Finally, it is unconventional to put home directories in the /mnt folder. That folder has a special purpose when mounting file systems that are not normally mounted (things like CD's, USB drives, etc.). Using that folder for these home directories will probably lead to unexpected issues if/when someone mounts a drove over /mnt and all your home folders disappear.

The end code would be something like (untested):

#! /bin/bash 
# exit if there's a failed command
set -e
if [ $# -lt 2 ] 
 then 
 echo -e "mkve - make virtual environment\nUsage: mkve project_name user_name" 
 exit 2
fi
pname=1ドル
puser=2ドル
echo project name: $pname
echo project user: $puser
# create the webapp user and its home directory, and give it ownership 
sudo useradd --system --gid webapps --shell /bin/bash --home /ves/$pname $puser 
sudo mkdir -p /mnt/u1/$pname
sudo chown $puser /mnt/u1/$pname
# become the webapp user, create a virtual environment, start it, and install gunicorn and flask 
sudo --user $puser bash -c "cd ~; virtualenv ./venv" 
sudo --user $puser bash -c "cd ~;. venv/bin/activate; pip install gunicorn flask"
echo -e "Done! Start with:\n$ sudo -u $puser -i\n$ . venv/bin/activate" 
echo -e "Or undo the whole kaboodle with:\n$ sudo userdel -r $puser" 
# at this point, manual configuration is necessary, which is different for each project. 
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419
Loading
lang-bash

AltStyle によって変換されたページ (->オリジナル) /