Is there any way of splitting the script (Noob Here).
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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.
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:
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)
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
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.
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
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.
Last edited by r3sistance; 05-24-2011 at 12:49 AM.
Reason: clarity
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 but i understand that sometimes scripts can get to long and because of that they become unreadable
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.