Solved

Shell scripting review

Posted on 2013-12-22
3
289 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
3 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 400 total points
Comment Utility
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 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 100 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now