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: Code:
# test_file |
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 if it's not what you're looking for, press 'n' (next) goodluck :hattip: |
Quote:
|
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 Code:
echo $MYSTR Code:
the quick brown fox jumps over the lazy dog 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. |
Quote:
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 ;) |
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 |
All times are GMT -5. The time now is 02:03 PM. |