Sort code in unix bash scripting

I use object oriented programming. Now I just started learning unix bash scripts through linux.

I have a unix script with me. I wanted to break it down into “modules” or, preferably, programs like “more”, “ls”, etc., and then use the channels to connect all my programs together. For example, "some input" myProg1 | myProg2 | myProg3.

I want to organize my code and make it more accurate, and not all in one script. In addition, testing and development will be easy.

Can this be done, especially as a beginner?

+7
linux unix bash
source share
3 answers

There are a few things you might want to pay attention to, such as using aliases in bash and storing them in bashrc or in a separate file called bashrc

which will facilitate the execution of commands ..

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

You can also look at the use of functions in your code (many bash scripts in the home folder above the link to understand the browsing functions of this site ), which has much better examples ...

Take a look at some pipeline tails in a tail tail output script in another script

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

It is not clear why all the pipes can help in any case:

 ./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 

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 have in mind can be functions, and each function can call each other .. and then you give the script the value that it passes through the functions.

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

I also separated functions that could be obtained by another script to execute the same functions.

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

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

There are many useful .bashrc functions that contain many functions that also give some insight into how to simplify many daily recurring commands, such as requiring a connection, an alias can be written to do all of them for you ..

+5
source share

You can do one thing. Since C program can be divided into a header file and a source file to reduce complexity, you can split your bash script into two scripts - the header and the main script, but with some differences.

Header file. This will contain all common variables, defined and defined functions that will be used by your main script.

Your script - This will only contain function calls and other logic. You need to use the "source <" path to the header ">" in the script at startup to get all the functions and variables declared in the header available for your script.

+2
source share

Shell scripts have standard input and output, like any other Unix program, so you can use them in pipes. Sharing your scripts is a good solution, because you can use them later in pipes with other commands.

I organize Bash projects as follows:

  • Each command is placed in its own file.
  • Reusable functions are stored in a library file, which is a classic script with only functions
  • All files are in the same directory, so teams can find the library with $(dirname $0)/library
  • The configuration is stored in another file as environment variables

To make everything clear, you should not use global variables to communicate between functions and the main program.

I am preparing a template for scripts with the following prepared parts:

  • Title with name and copyright.
  • Read the configuration with source
  • Download the library using source
  • Check parameters
  • The function of displaying help caused by a request or an incorrect parameter

My best advice: always write a help function, as the next person who will be needed will be ... on your own!

To install the project, you simply copy all the files and explain what needs to be configured in the configuration file.

0
source share

All Articles