LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2019, 09:14 PM   #1
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Rep: Reputation: 13
Talking 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

# create directories
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}

# move dotfiles into docs/dev and move back into dotfiles
cd $HOME && mv dotfiles docs/dev && cd docs/dev/dotfiles

# install config
cd config

cp -r dunst $HOME/.config/dunst
cp -r irssi $HOME/.config/irssi
cp -r mpd $HOME/.config/mpd
cp -r mpv $HOME/.config/mpv
cp -r nvim $HOME/.config/nvim
cp -r zathura $HOME/.config/zathura

# install home
cd ../home

cp -r .Xresources $HOME/.Xresources
cp -r .ksh_history $HOME/.ksh_history
cp -r .kshrc $HOME/.kshrc
cp -r .mpd $HOME/.mpd
cp -r .ncmpcpp $HOME/.ncmpcpp
cp -r .profile $HOME/.profile
cp -r .scripts $HOME/.scripts
cp -r .tmux.conf $HOME/.tmux.conf
cp -r .w3m $HOME/.w3m
cp -r .xsession $HOME/.xsession
doas cp -r fonts/* /usr/local/fonts/
doas cp -r systemerror.png /usr/local/share/backgrounds/

echo "dotfiles installed"
I removed my edits as they were from me not paying attention and would just be confusing.

Last edited by FOSSilized_Daemon; 06-16-2019 at 02:34 PM.
 
Old 06-14-2019, 10:17 PM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,264

Rep: Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106
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 .
 
1 members found this post helpful.
Old 06-14-2019, 10:28 PM   #3
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by scasey View Post
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 .
There is only one thing that isn't working. So I am testing this on a Linux Mint machine and I am looking to replace all the cp -rs for config to just be
Code:
cp -r config/. $HOME/.config
when I run that manually it works, but when I run it in a script it says
Code:
cp: cannot stat 'config/.': No such file or directory
I am confused why it keeps saying that only when in a script
 
Old 06-14-2019, 11:19 PM   #4
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,264

Rep: Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106
Quote:
Originally Posted by FOSSilized_Daemon View Post
There is only one thing that isn't working. So I am testing this on a Linux Mint machine and I am looking to replace all the cp -rs for config to just be
Code:
cp -r config/. $HOME/.config
when I run that manually it works, but when I run it in a script it says
Code:
cp: cannot stat 'config/.': No such file or directory
I am confused why it keeps saying that only when in a script
That command says “copy the file named . in the config directory ” ...there is no such file
Do you mean
Code:
cp -r config/.*
.?? perhaps

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.

Last edited by scasey; 06-15-2019 at 12:38 AM.
 
Old 06-15-2019, 06:53 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,474

Rep: Reputation: 1759Reputation: 1759Reputation: 1759Reputation: 1759Reputation: 1759Reputation: 1759Reputation: 1759Reputation: 1759Reputation: 1759Reputation: 1759Reputation: 1759
Quote:
Originally Posted by FOSSilized_Daemon View Post
Code:
# move dotfiles into docs/dev and move back into dotfiles
cd $HOME && mv dotfiles docs/dev && cd docs/dev/dotfiles
This will fail to cd back if the mv fails for any reason. I suggest a subshell instead:
Code:
(cd $HOME && mv dotfiles docs/dev)
Quote:
Originally Posted by scasey View Post
That command says “copy the file named . in the config directory ” ...there is no such file
Every directory contains a hard link to itself named ".", so I think this should work.

Last edited by ntubski; 06-15-2019 at 06:54 AM. Reason: add [code] tag to quote
 
1 members found this post helpful.
Old 06-15-2019, 09:09 AM   #6
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,264

Rep: Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106
Quote:
Originally Posted by ntubski View Post

Every directory contains a hard link to itself named ".", so I think this should work.
Oh! Excellent point. Then I suspect the issue is that the source dirs are not readable by the user running the script.
OR there is no directory in the current directory named config...

Last edited by scasey; 06-15-2019 at 09:12 AM.
 
Old 06-15-2019, 09:43 AM   #7
individual
Member
 
Registered: Jul 2018
Posts: 212

Rep: Reputation: 155Reputation: 155
Here is a simplified version of your script, with comments.
Code:
# Just specify $HOME once.
mkdir -p "$HOME"/{docs,docs/notes,docs/pdfs,docs/dev,dl,vids,music,pics,.config,.builds}

# Move dotfiles into the $HOME/docs/dev directory, without cd'ing into $HOME
# EDIT: This line seems unnecessary since you could just stay in your original dotfiles directory.
# mv dotfiles "$HOME/docs/dev"

# Find is a really useful command that everyone should be at least somewhat familar with.
# You have to specify the regex type as egrep or awk if you want to alternate patterns '|'.
# EDIT2: If you aren't in the dotfiles directory you'll need to specify it.
find dotfiles -regextype egrep -regex '.*(dunst|irssi|mpd|mpv|nvim|zathura)' -exec cp -r \{} "$HOME/.config" \;

# This is the same as above except for the destination directory, which is $HOME.
find dotfiles -regextype egrep -regex '.*(.Xresources|.ksh_history|.kshrc|.mpd|.ncmpcpp|.profile|.scripts|.tmux.conf|.w3m|.xsession)' -exec cp -r \{} "$HOME" \;

Last edited by individual; 06-15-2019 at 09:54 AM.
 
Old 06-15-2019, 10:41 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware 14.2 current / ArcoLinux / Void Linux
Posts: 8,857

Rep: Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851
I agree you should to absolute paths and or reserved words HOME USER etc... as needed.
dealing with dot files
Code:
#!/bin/bash

#create a dir called temp add this file and chmod +x filename
#then run it

#declare your dirs you will be working with

dofiles=$HOME/temp/dotfiles
CopyDOTSto=$HOME/temp/CopyDotsHere

#dynamically create dirs to be used
mkdir -pv $dofiles
mkdir -pv $CopyDOTSto

#create some test dot files
touch $dofiles/.{a..z}.txt

# for considering dot files (turn on dot files)
#bash build in
shopt -s dotglob 

#change dir into where dotfiles are located
cd $dofiles

#echo location you're at now just for piece of mind
echo $(pwd)

#now that you're in the dot file directory
#copy all of the dot files into where I want them
cp -rv * $CopyDOTSto

#show results
ls -la $CopyDOTSto
when in doubt double quote "$var"

Last edited by BW-userx; 06-15-2019 at 11:11 AM.
 
Old 06-16-2019, 06:04 AM   #9
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 366

Rep: Reputation: 140Reputation: 140
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.
 
Old 06-16-2019, 06:39 AM   #10
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 2,948

Rep: Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265
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.
 
1 members found this post helpful.
Old 06-16-2019, 08:17 AM   #11
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,687

Rep: Reputation: Disabled
Quote:
Originally Posted by wpeckham View Post
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.
For example the brace expansions are a bash feature and not portable.

In bash.

Code:
$ echo {foo,bar}
foo bar
And with dash.

Code:
$ echo {foo,bar}
{foo,bar}
 
2 members found this post helpful.
Old 06-16-2019, 01:29 PM   #12
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by orbea View Post
For example the brace expansions are a bash feature and not portable.

In bash.

Code:
$ echo {foo,bar}
foo bar
And with dash.

Code:
$ echo {foo,bar}
{foo,bar}
I did not know that was a bashism, see this is why I hate so many beginner books so much. Don't tell me that is a Linux thing if it is in fact a bashism. Thank you for the heads up.
 
Old 06-16-2019, 01:42 PM   #13
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by orbea View Post
For example the brace expansions are a bash feature and not portable.

In bash.

Code:
$ echo {foo,bar}
foo bar
And with dash.

Code:
$ echo {foo,bar}
{foo,bar}
Do you by chance know of a POSIX way to do something similar to that or do I have to just do them one at a time?

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

Last edited by FOSSilized_Daemon; 06-16-2019 at 02:15 PM.
 
Old 06-16-2019, 02:32 PM   #14
Mechanikx
Member
 
Registered: Jul 2018
Location: Canada
Distribution: Slackware
Posts: 150

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by FOSSilized_Daemon View Post
I did not know that was a bashism, see this is why I hate so many beginner books so much. Don't tell me that is a Linux thing if it is in fact a bashism. Thank you for the heads up.
If you're interested you should check out Unix Shell Programming 4th edition (Shell Programming in Unix, Linux and OS X) by Kochan and Wood:

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.
 
Old 06-16-2019, 02:34 PM   #15
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by Mechanikx View Post
If you're interested you should check out Unix Shell Programming 4th edition (Shell Programming in Unix, Linux and OS X) by Kochan and Wood:

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.
Thank you so much, I will definitely get this!
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can someone explain for me this shell script please? (It was detected on my honeypot) haz12 Linux - Security 8 05-02-2016 11:34 AM
How can you tell that someone is bad? or Good ? someone is bad? or Good ? abrenar General 10 02-24-2009 02:42 PM
Cloning disk partitions; could someone review this? mlitwinowich Linux - General 2 10-03-2008 01:17 PM
Could someone tell me what's wrong with my shell script please? RowanB Programming 4 11-11-2004 11:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration