Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

[Help!] Bash Shell Scripting Functions


MISSION:

I'm creating bash scripts that will do a number of tasks as an amateur server admin (Root).

Any tips on achieving any the different functions are greatly appreciated. I could use all the help I can get! I'll have a rough notes/sources section and will be making mini-scripts to test each function so I can then add them to the final master scripts.

BRIEFING:

-All scripts must be accessed by root only (See Solved! :D)

-Create two new CLI commands 'lock' and 'unlock' from scripts named 'lock' and 'unlock' in /usr/local/sbin/. The 2 master scripts can be made up of multiple scripts as long as there are two main ones which root operates (lock/unlock).

-lock/unlock has the option to work with input from files

Lock/Unlock File Input Example
Spoiler (Click to View)

LOCK/UNLOCK NECESSARY REQUIREMENTS

-Prompt for and read in any number of account ids from stdin (one per line) to be locked or unlocked; detect end-of-file (Control-D) in order to terminate the script. This is the no arguments case.

#Example:
Spoiler (Click to View)

-Use of the passwd -l and passwd -u commands to lock accounts in the script.

-Use any number of arguments as account ids to be locked or unlocked with no practical limit to the number of accounts that can be supplied as long as the command line does not exceed 256 characters total.

#Example

Spoiler (Click to View)

-Use command-line arguments to determine input method:
If no arguments, ask for an account or list of accounts one at a time from stdin (Solved)
If 2 arguments and the first is -f, read the list of accounts one at a time from the second argument as a file
Otherwise, use the arguments as the list of accounts

In each case, process each item of the list of accounts as follows:
-Accounts must exist
-Examine password for valid-locked or valid-unlocked ("man lock/unlock" command mentions this, will add to rough notes for explanation)
-If command is lock and password is valid-unlocked, lock it
-If command is unlock and password is valid-locked, unlock it
-Issue messages and log information as required.
*(echo, logger/rsyslog.con edit)



SOLVED
Root access only to scripts:
Spoiler (Click to View)


ROUGH NOTES
To check if username is exists, search /etc/passwd file
Example:
grep ^user /etc/passwd

Default arguments $0,$1,$2,etc..
$0= file name
$1= first argument
$2= second argument
etc...
$@ and $#= all arguments. They have subtle differences


FINISHED SCRIPTS
This space is reserved for finished scripts. They have been tested and work 100% of the time.

Example Test Script 1: Root Access
Spoiler (Click to View)
Example Test Script 2: Verify User Existance
Spoiler (Click to View)
[spoiler][/spoiler]
[code][/code]

Those two are for code snippets and spoilers.
Thanks! Editing now..
I'll get the hang of forums sooner or later..


Updated the rough notes with some useful info.

Feel free to post any hints, tips or suggestions!


Adding to scripts now... two have been made so far for testing phase


Possibly Related Threads...
Thread / Author Replies Views Last Post
by SIM-5610537Jan 18, 2025 05:19 PM 5 316 Jan 18, 2025 05:19 PM
Last Post: SIM-5610537
by yungchristMay 12, 2017 08:21 PM 3 268 May 12, 2017 08:21 PM
Last Post: g4143
by kingpzzoJan 9, 2016 04:28 PM 0 109 Jan 9, 2016 04:28 PM
Last Post: kingpzzo