Showing posts with label scripts. Show all posts
Showing posts with label scripts. Show all posts

Special Shell Variables - Useful for Scripting

Below are the special shell variables. These are important to know for everyone, especially who is willing to learn shell scripting. Hope the list helps.

Name
Description
$1 - $9
These variables are referring the parameters passed to command or script. $1 refers to the first argument and $2 refers second and so on.
$0
The name of the command or script currently being executed.
$#
The number of arguments passed to the command/script or invocation of the shell.
$?                     
The exit status of the last command executed is given as a decimal string.  When a command completes successfully, it returns the exit status of 0 (zero), otherwise it returns a non-zero exit status.
$$
The process number of the currently executing command or script. - Useful for including in filenames, to make them unique.
$!
The process ID of the last command runs in the background.
$-
The current options supplied to the command or script.
$*
A string containing all the arguments passed to the command/script or shell, starting at $1. When quoted, "$*" is a single word, comprising all the arguments to the shell, joined together with spaces. For example ‘a b' c becomes "a b c".
$@
Same as above, except when quoted. When quoted, "$@" is identical to the arguments received by the shell, the resulting list of words completely match what was given to the shell. For example '1 2' 3 becomes "1 2" "3"

Apart from these, there are some standard variables which are set through .profile or .bashrc. Try "env" command to view all of them.

Script to find out particular file type in UNIX

In day to day system administration tasks, In some situations, you may need to find out a particular file type (ex: script files, regular files, etc..)  in large file systems. I had a situation to find out only script files (Shell and Perl Scripts) which are using "rsh" in the script code in a very large file system which contains more than 1,00,000 files.


I wrote a simple script to achieve this task. I am sharing this here, because this may be helpful to others as well.This script can be altered little to find out other file types as well.



#!/bin/ksh
find /GIS -type f | while read filename
 do
 filetype=$(file $filename | awk -F: '{print $2}'| awk '{print $2}')
 if [ "$filetype" == "script" ];then
    grep -i rsh $filename > /dev/null
    if [ $? -eq 0 ];then
       echo "$filename" >> /tmp/rsh_scripts_report
    fi
 fi
done

Login Script: Inventory Collection for domain computers

Here is the another script to collect the asset inventory of domain Computers over network. it will collect the Hardware and Software information of the Windows PC/Laptops which are logging into domain.


This is the easy to collect asset inventory of your networked Windows PC/Laptops. 

It will create two files for each computer: 





1 A file contains hardware details (Serial no, Model No, User, etc..)
2. Another file contains list of installed softwares on the computers


You can use txtcollector tool to merge all these files into one. you can easily import into excel and customize your report.


Place this script as your login script. or place this script in network drive and call it from your master login script.
Create a new group policy for this and assign to entire domain.


Modify the Tombstone lifetime

I would include a few PowerShell scripts here that can be used to modify the tombstone lifetime along with the deleted object lifetime. Remember that the default for both of these is going to be 180 days and will show up as Null if you use LDP to view the attributes.

PowerShell Script to change the tombstone lifetime of my domain (AdminPrep.Local) to 250 days:

Set-ADObject -Identity “CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=AdminPrep,DC=Local” –Partition “CN=Configuration,DC=AdminPrep,DC=Local” –Replace:@{“tombstoneLifetime” = 250}

PowerShell Script to change the deleted object lifetime:

Set-ADObject -Identity “CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=AdminPrep,DC=Local” –Partition “CN=Configuration,DC=AdminPrep,DC=Local” –Replace:@{“msDS-DeletedObjectLifetime” = 250}