LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Is there any way of splitting the script (Noob Here). (https://www.linuxquestions.org/questions/linux-newbie-8/is-there-any-way-of-splitting-the-script-noob-here-882234/)

pinga123 05-23-2011 06:46 AM

Is there any way of splitting the script (Noob Here).
 
I m writing a script to check Server Hardening.
The problem is whenever i add new point it grows and it become very tedious to edit the script file.

Is there any way of making them separate and call them from one base script?

Is it possible to define global variable that can be accessed via all the scripts?

Never done this kind of scripting little reference would be appreciated.
(The idea come to my mind while watching the execution of lynis tool the scripts were given different tasks and all were called from a main base script)

acid_kewpie 05-23-2011 06:49 AM

totally, just move the commands to a different file and call it like any other script / command. You might like to look at some sort of numerical ordering thing to automatically pull in files in a certain order, but that's window dressing. Only gotcha I can really think of is where the scripts are. If you're already running ./myscript then running ./myotherscript" inside it will work fine.

grail 05-23-2011 07:56 AM

Firstly pinga, I am not sure you can still claim noob-dem with all the work and posts I have seen of yours :)

As acid pointed out it is the same as calling any other command, it will mainly depend on where they are located.
Another suggestion is you could also place your snippets into functions and then source the file with those in (create a temp file while testing is being performed).
Example:
Code:

# test_file
my_test()
{
  echo "Just testing here:)"
}

# script.sh file
[[ -f /path/to/test/file/test_file ]] && . /path/to/test/file/test_file

<part of script>
my_test
<rest of script>

Rough, but you get the idea.

brownie_cookie 05-23-2011 08:02 AM

like grail said, you can put some parts in functions (you give the functions a name)
and if you want to edit the file and it's too large, or not fun to read, you can search for things
maybe that is a better way than splitting scripts? but i don't know if that is what you're looking for

anyway, you can do that as the following:
Code:

# vi /folder/name.txt
press '/' and type the word or letter or whatever you want to look for and hit enter
if it's not what you're looking for, press 'n' (next)

goodluck

:hattip:

pinga123 05-23-2011 11:14 PM

Quote:

Originally Posted by brownie_cookie (Post 4364518)
like grail said, you can put some parts in functions (you give the functions a name)
and if you want to edit the file and it's too large, or not fun to read, you can search for things
maybe that is a better way than splitting scripts? but i don't know if that is what you're looking for

anyway, you can do that as the following:
Code:

# vi /folder/name.txt
press '/' and type the word or letter or whatever you want to look for and hit enter
if it's not what you're looking for, press 'n' (next)

goodluck

:hattip:

I Thought of doing it but after watching the execution of lynis tool i was little confused about best way of scripting .If putting everything in a one script using function is a preferred way then i might not want to change my scripting style.

chrism01 05-24-2011 12:10 AM

Typically, you'd structure it using fns (like any other decent lang eg C, Perl etc).
You're more likely to invoke separate scripts if you want things done in parallel.
At the end of the day though, it's your choice.
Just be careful of scoping rules.
http://steve-parker.org/sh/functions.shtml

r3sistance 05-24-2011 12:46 AM

I believe the answer to this is '. /path/to/script'

I'll give an example since that might not be the best way to explain it, a practical example might be better, for this assume we have two scripts, quickscript and quickfunc what both are in the same directory

quickscript
Code:

#!/bin/bash
MYSTR="the quick brown fox jumps over the lazy dog"
. quickfunc
echo $MYRAN

quickfunc
Code:

echo $MYSTR
MYRAN="works this way to"

when quickscript is run, this will output
Code:

the quick brown fox jumps over the lazy dog
works this way to

I assume this is what you are after?

if not then perhaps it might be an idea just to have separate files for each major function and use a separate script that uses cat to merge one main file out of the smaller ones. This is better then remembering the order manually and manually merging them.

brownie_cookie 05-24-2011 12:50 AM

Quote:

Originally Posted by pinga123 (Post 4365234)
I Thought of doing it but after watching the execution of lynis tool i was little confused about best way of scripting .If putting everything in a one script using function is a preferred way then i might not want to change my scripting style.

imo the best way of scripting is to put everything in one script, and try to make it so readable as you can, even when the script is 'miles' long.
Because, when you have 2 or more scripts/files, than you or any other person has to check multiple files for debugging or whatever...

but that's only my opinion :p but i understand that sometimes scripts can get to long and because of that they become unreadable

good luck ;)

catkin 05-24-2011 02:56 AM

You can set up one "script" for use as a library by the others -- setting common variables and defining common functions -- and then source it into the main scripts. Here's a sample header
Code:

# Filename: bash_lib.sh

# Purpose:
#  * Common code library for bash scripts

# Usage:
#  * Source this file via the . or source command
#  * Global variables referenced by functions in this library:
#    - $debugging: set to $true or $false. Set by library user.
#    - $false: set by this library
#    - $global_error_flag: set by this library's msg function when called with msgclass E
#    - $global_warning_flag: set by this library's msg function when called with msgclass W
#    - $have_tty: set to $true or $false. Set by this library.
#    - $my_nam: caller's short name.  Set by this library.
#    - $redirect_afn: name of file to receive any redirection. Set by this library.
#    - $true: set by this library

#  * Functions called by functions in this library:
#    - fct: function call trace, called by some functions in this library debuuging. In this library.
#    - finalise: clean up and exit. Provided by library user.
       
# Configure shell
# ~~~~~~~~~~~~~~~
export PATH=/usr/sbin:/sbin:/usr/bin:/bin
set -o nounset
shopt -s extglob nullglob
umask 0022
unalias -a

# Utility variables
# ~~~~~~~~~~~~~~~~~
readonly false=
readonly my_nam=${0##*/}
readonly true=true

# Logic variables
# ~~~~~~~~~~~~~~~
global_error_flag=$false
global_warning_flag=$false

# Determine whether there is a terminal available for output
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The "standard" test is to check $PS1 but this test is more reliable
have_tty=$false
case "$( /bin/ps -p $$ -o tty 2>&1 )" in
    *TT*-* | *TT*\?* )
        ;; 
    *TT* )
        have_tty=$true
esac

# Set traps
# ~~~~~~~~~
trap 'finalise 129' 'HUP'
trap 'finalise 130' 'INT'
trap 'finalise 131' 'QUIT'
trap 'finalise 143' 'TERM'

# Function definitions
# ~~~~~~~~~~~~~~~~~~~~
# In alphabetical order:
# * ck_file
# * ck_integer
# * fct
# * msg



All times are GMT -5. The time now is 02:03 PM.