Solved

Shell scripting review

Posted on 2013-12-22
3
309 Views
Last Modified: 2014-01-05
Hi ,

I have written a script based on collegues description, i just not sure whether it was well written .

I need your expert advice on the below code to check for any mistakes since i dont have the setup to test here in my environment.

#!/usr/bin/sh
#written by magento
#Code written based on assumptions below
# $1 = cointype - It is the directory created using (mkdir /home/coinname/.coinname)
# $2 = github url (https://github.com/coinname/coinname)
# $3 = node ip added in coinname.conf (echo "addnode=$3")
#validating the arguments 
if test $# != 3
then
    echo "Usage $0: coin_type github_location  coin_upstream_node_ip" 1>&2
    exit 1
fi
#generating random password
random_passwd=`tr -dc A-Za-z0-9_ < /dev/urandom | head -c 16 | xargs`
#steps as per description
adduser poolname $random_passwd
echo "user added with password:$random_passwd"
cd /home/poolname
git clone $2
cd coinname/src
make -f makefile.unix USE_UPNP=-
chown coinname:coinname /home/coinname
mkdir /home/coinname/$1
#declaring conf filepath
conf=/home/coinname/$1/coinname.conf
#consolidating the conf 
function generate_coinconf
{
	echo "rpcuser=coinnamerpc" >> $conf
	echo "rpcpassword=43462346236090980423423256342" >> $conf
	echo "rcpallowip=*" >> $conf
	echo "server=1" >> $conf
	echo "listen=1" >> $conf
	echo "daemon=1" >> $conf
	echo "addnode=$3" >> $conf
 }
 #writing the conf
 generate_coinconf
#simply running coinname
sudo su coinname
./coinname 2>&1 > /home/coinname/$1/coinname.log
#getting the port
echo "port details below:"
./coinname --help | awk 'NF > 0 { print $1 }' test.txt | sed -ne '/-port/,/-rpcport/p'
#finally controversing alias cp
\cp -R /home/template/
echo "done"

Open in new window

0
Comment
Question by:magento
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 400 total points
ID: 39735566
Few specific points:
- when setting random_password, you don't need the xargs at the end.

- I don't think that having a function "generate_coinconf" adds much to the script - you could just as easily have the lines in the code.  You might do this to minimise clutter (just a simple "generate_coinconf" rather than lots of lines of text), but you would then move the function definition away (at the top of the script?).

- "sudo su coinname" would try, as root, to become the coinname user, but then be at a  shell prompt.  I assume that's not what you want. should the text on the next line (./coinname...') be on this line?

- in the first line that runs ./coinname, standard error will be sent to the console, and only standard out will be sent to the log file.  Is this what you want?  If you want both stdout and stderr to go to the log file, reverse the redirection statements, so you have
    ./coinname > /home/coinname/$1/coinname.log 2>&1

- In the second line that runs ./coinname, the awk line has a file to process (test.txt) so it won't process its standard input - the output from "./coinname --help" will be thrown away.

- Aliasses don't apply within shell scripts, sothere's no need for the \ in \cp.

- The "cp" doesn't have a target, just a source (/home/template).

Otherwise, my main comment would be that you have nowhere near enough error checking.  What happens if the adduser fails?  What if there is a problem running the "make"?  What if the coinname user doesn't already exist?
0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 100 total points
ID: 39735788
You want to know whether your script is 'well written' - this is subjective, not objective. So you need to ask yourself what you consider well. As the above post is pointing out already, there are some syntax and logical issues in your script, they don't add to 'well' written scripts.

In general, I would:

- Start with pseudo code, this will save you time in the end and may prevent logic errors
- Think of how you want to test your script, write test scenarios if you can, including extreme cases (like 0 parameters, 3 parameters and 10 parameters for example)
- Write small piece of code, test
- Put pieces together, test

I usually keep my pseudo code as a comment.
0
 
LVL 5

Author Comment

by:magento
ID: 39736312
Hi Simon,

Thanks for your advice , i will correct those redirections ,function and alias.

I assumed there wont be any problem with add user and those stuffs.

The awk need to work with the ./coinname --help only so i will change that too .

Thanks
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question