Need Someone to Review my Shell Script
I have been learning shell scripting for the past few days and decided to write my first script. Basically this script just creates all my directories I need in my home directory and installs my dotfiles. I was wondering if anyone could take a look at my shell script and give me some feedback, what can be done in a more minimal fasion and just if there is anyway I can improve my script. Thank you in advance, here it is:
Code:
#/bin/sh |
Personally, I’d use absolute paths in that script, mostly for clarity....I’ll concede that’s a style preference. The style you’re using with cd commands is not “wrong”, I just find it hard to follow.
Using $HOME means the script expects to be run by the user being configured. Yes? I’d put a comment in noting that and insure the script permissions and all source directories permissions are as they need to be. Bottom line is: Does it work? Does it do what you expect? More minimal? adduser can be configured (defaults?) to copy defaults from /etc/skel/*. If that’s set up as you want, the script is not needed at all ;). |
Quote:
Code:
cp -r config/. $HOME/.config Code:
cp: cannot stat 'config/.': No such file or directory |
Quote:
Do you mean Code:
cp -r config/.* Or, to my original point...is there a config directory in the in the directory the script is in when the command is executed? Or my second original point, is the directory being copied readable by the user running the script? Use set -x to see what’s happening. |
Quote:
Code:
(cd $HOME && mv dotfiles docs/dev) Quote:
|
Quote:
OR there is no directory in the current directory named config... ;) |
Here is a simplified version of your script, with comments.
Code:
# Just specify $HOME once. |
I agree you should to absolute paths and or reserved words HOME USER etc... as needed.
dealing with dot files Code:
#!/bin/bash |
I'd suggest using a lint-like tool for shell scripts called Shellcheck available at https://www.shellcheck.net and its command line interface https://github.com/koalaman/shellcheck.
Of course it's just the first step in reviewing the code and perhaps the only one that can be done automatically but at least you can get rid of the most visible and obvious bugs at the very early stage. |
I noticed that your shebang "#!" line specified the sh shell. If you are using a different shell for your session (bash, perhaps) the behavior may differ due to the existence or behavior of different shell internals. Even if it is the same shell but called under a different name, named as /bin/sh activates the posix behavior for most shells, and that CAN make a difference. Here it should not, but it is a factor you should always consider.
|
Quote:
In bash. Code:
$ echo {foo,bar} Code:
$ echo {foo,bar} |
Quote:
|
Quote:
edit: mkdir -p $HOME/docs $HOME/docs/notes $HOME/docs/pdfs $HOME/docs/dev $HOME/dl $HOME/vids $HOME/music $HOME/pics $HOME/.config $HOME/.builds |
Quote:
https://www.amazon.ca/Shell-Programm...gateway&sr=8-1 Its focus is the posix shell. And near the end it discusses some features unique to common shells. From what I remember all the examples, or most, are tested in multiple different shells, bash, ksh, etc. |
Quote:
|
All times are GMT -5. The time now is 12:57 AM. |