LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-23-2011, 06:46 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
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)
 
Old 05-23-2011, 06:49 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 05-23-2011, 07:56 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
 
Old 05-23-2011, 08:02 AM   #4
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
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

 
Old 05-23-2011, 11:14 PM   #5
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by brownie_cookie View Post
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.
 
Old 05-24-2011, 12:10 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,361

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 05-24-2011, 12:46 AM   #7
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
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
 
Old 05-24-2011, 12:50 AM   #8
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
Quote:
Originally Posted by pinga123 View Post
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

good luck
 
Old 05-24-2011, 02:56 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Log splitting script chris Programming 6 10-14-2009 09:21 AM
File splitting script stephencassidy Programming 3 03-23-2007 12:09 PM
Script: splitting lines in multiple files and joining them timmay9162 Programming 28 04-14-2006 08:52 AM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 03:20 AM
script noob spuppett Linux - General 1 04-27-2004 09:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration