amuck-landowner

Shell scripting 101

I was wondering what is a good source for how to learn shell scripting? I really want to become proficient in it. Here is example of my work i wrote to get the ipv4 side of the network running correctly in fedora 21 beta openvz template. If there is anything you can point out i would greatly appreciate it.

 #!/bin/bash

# grabbing ip from ifcfg

for address in $(cat /etc/sysconfig/network-scripts/ifcfg-venet0:0 | grep -o 'IPADDR=[^\n]*' | cut -f2- -d'=')

do

   /sbin/ip addr add $address/30 broadcast $address dev venet0:0

 

done

 
 

MannDude

Just a dude
vpsBoard Founder
Moderator
Good question. I'd like to know also, as the only thing I've "made" in bash is this: http://git.vpsboard.com/MannDude/simple-bash-backup-script/snippets/5 and it's nothing special and may not actually work. I wrote that last year and just recently re-discovered it.

Then again I have a habit of introducing myself to things such as Python and PHP, getting far enough it it that I can make something stupid/useless and have a very limited understanding... then I don't touch it again forever and forget it all. I also dabbled with CGI/Perl back in like... 2004 when I ran a YaBB forum and all that is gone/lost also =/
 
Last edited by a moderator:

splitice

Just a little bit crazy...
Verified Provider
Gotta admit I never really I learnt it until I had to produce my first scripts to do certain things. Once I got over the hurdle of the pipes & filters style it all sorta seemed to make sense.

Off the top of my head. A few things to remember:

  1. Treat everything as a string unless you have specifically typed it otherwise
  2. All variables being used belong in quotes, i.e never echo $a always echo "$a"
  3. Bash is nothing without the collection of posix (and other) utilities that you can call. Specifically learn awk & grep those two are very useful in getting more power.
  4. Its quite common to take data and combine it into a string, then use another utility to do some further manipulation on the string (i.e regex) before extracting it back out.
  5. Beware of "bashisms" (i.e arrays) if you want cross platform support.
 

tonyg

New Member
Right now it might not mean much, but learn cross-platform compatible commands...stay away from bash specific commands (bashism).

In other words, make sure your scripts work with /bin/sh and not just with /bin/bash.
 

Abdussamad

New Member
If you go through that tldp guide you'll see that there are like 5 different types of if statements in bash. What evaluates to true or false also varies. Sometimes 1 is a true and sometimes it is a false depending on the construct you use. It's confusing as hell.
 
Top
amuck-landowner