Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organize code in unix bash scripting

Tags:

linux

bash

unix

I am used to object oriented programming. Now, I have just started learning unix bash scripting via linux.

I have a unix script with me. I wanted to break it down into "modules" or preferably programs similar to "more", "ls", etc., and then use pipes to link all my programs together. E.g., "some input" myProg1 | myProg2 | myProg3.

I want to organize my code and make it look neater, instead of all in one script. Also, it will be easy to do testing and development.

Is it possible to do this, especially as a newbie ?

like image 276
bashboy Avatar asked Jul 30 '13 19:07

bashboy


People also ask

Do bash scripts run in order?

Yes, they are executed sequentially. However, if you run a program in the background, the next command in your script is executed immediately after the backgrounded command is started.

What is ${} in bash script?

In this tutorial, we will explain two of the most useful bash expansions used in shell scripts: $() – the command substitution. ${} – the parameter substitution/variable expansion.

How do I make my bash script better?

The best way to learn bash is to learn the command line first and then work on writing bash scripts. You can take a good bash course and practice writing scripts as much as possible.


1 Answers

There are a few things you could take a look at, for example the usage of aliases in bash and storing them in either bashrc or a seperate file called by bashrc

that will make running commands easier..

take a look here for expanding commands into aliases (simple aliases are easy)

You can also look into using functions in your code (lots of bash scripts in above link's home folder to make sense of functions browse this site :) which has much better examples...

Take a look here for some piping tails into script pipe tail output into another script

The thing with bash is its flexibility, so for example if something starts to get too messy for bash you could always write a perl/Java any lang and then call this from within your bash script, capture its output and do something else..

Unsure why all the pipes anyways here is something that may be of help:

./example.sh 20
function one starts with 20
In function 2 20 + 10 = 30
Function three returns 10 + 10 = 40
------------------------------------------------

------------------------------------------------
Local function variables global:
Result2: 30 - Result3: 40 - value2: 10 - value1: 20

The script:

example.sh

#!/bin/bash

input=$1;

source ./shared.sh

one

echo "------------------------------------------------"



echo "------------------------------------------------"
echo "Local function variables global:"
echo "Result2: $result2 - Result3: $result3 - value2: $value2 - value1: $value1"

shared.sh

function one() {
        value1=$input
        echo "function one starts with $value1"
        two;
}

function two() {
        value2=10;
        result2=$(expr $value1 + $value2)
        echo "In function 2 $value1 + $value2 = $result2"
        three;
}
function three()  {
        local value3=10;
        result3=$(expr $value2 + $result2;)
        echo "Function three returns $value2 + $value3 = $result3"
}

I think the pipes you mean can actually be functions and each function can call one another.. and then you give the script the value which it passes through the functions..

bash is pretty flexible about passing values around, so long as the function being called before has the variable the next function being called by it can reuse it or it can be called from main program

I also split out the functions which can be sourced by another script to carry out the same functions

E2A Thanks for the upvote, I have also decided to include this link

http://tldp.org/LDP/abs/html/sample-bashrc.html

There is an awesome .bashrc to be reused, it has a lot of functions which will also give some insight into how to simplify a lot of daily repetitive commands such as that require piping, an alias can be written to do all of them for you..

like image 152
V H Avatar answered Sep 19 '22 13:09

V H