Categories

dynamic date value in shell scripts

dynamic date value in shell scripts using a variable – easier to read in scripts: NOW=$(date +”%Y%m%d%H%M%S”) echo “bar” > “$NOW”foobar.txt

one-line, equivalent to above: echo “bar” > $(date +”%Y%m%d%H%M%S”)foobar.txt

alternate single-line format: echo “bar” >> $(date “+myfile%Y%m%d.txt”)

man strftime to see formatting values

Calculating percentages in bash

Calculating percentages in bash Dividing in bash will cause problems if the result is below zero. This is a problem when you’re trying to work out percentages. For example, if you simply want to divide 1 by 2 the result should be 0.5. However, bash returns the result 0:

user@computer:~> echo $(( 1 / 2 […]

shell script for ssh session

#!/bin/bash # max_number_of_ssh_client  max_number_of_ssh_client=3 # lsof is essential if [[ ! -x `which lsof` ]]; then     yum install lsof -y     RETVAL=$?     if [[ “$RETVAL” -ne “0” ]]; then         echo “ERROR: can NOT use lsof command, please check your internet connection or install lsof by manual! ”         exit $RETVAL     fi else     # TODO     # for awk, grep, etc     echo “SUCCESS: This shell script will knock out extra ssh connection ” fi # a main worker # loop while : ; do     ssh_port=`netstat -anopt | grep sshd | awk ‘{print $4}’ | awk -F ‘:’ ‘{print $2}’ | grep -v ^$ | uniq `     ssh_clients=`lsof -i:$ssh_port | grep \> | awk ‘{print $9}’ | awk -F ‘:’ ‘{print $(NF-1)}’ | uniq | awk -F ‘>’ ‘{print $2}’`     for ssh_client in $ssh_clients; do         number_of_ssh_client=`lsof -i:$ssh_port | grep $ssh_client | wc -l`         if [[ $number_of_ssh_client -gt $max_number_of_ssh_client ]]; then             number_pts=`w -hs | grep $ssh_client | awk ‘{print $2}’ | awk -F ‘/’ ‘{print $2}’ | awk ‘BEGIN {max=0} {if ($1>max) max=$1 fi} END {print max}’`             # TODO             # another solution maybe exist             # kill extra logins             pkill -kill -t pts/$number_pts             if [[ $? -eq 0 ]]; then                 echo “SUCCESS: extra connections  $ssh_client@pts/$number_pts has been knocked out! ”             else                 echo “WARNNING: can NOT knock out extra connections! ”             fi         else             # TODO             # too many INFO displayed             echo “INFO: number of ssh connections is NORMAL! ”             # sleep 1             sleep 1         fi     done done […]

Clear contents of a file in UNIX

Clear contents of a file in UNIX

$cat /dev/null > file

or

$true > file

Viewing file systems that 90% and more using awk & sed

This works with AIX

df -g | egrep -v “Filesystem|proc|tmp” | sed ‘s/\%//’ | awk ‘$4 >= 90 {print $4 “%\t” $7}’

df -k | sed 1d | awk ‘$4>90 {print}’

This works with Solaris and Linux

df -k | egrep -v “proc|fd|cdrom|mnttab|run|tmp|Filesystem” | sed ‘s/\%//’ | awk ‘$5 >= 90 {print $5 “%\t” $6}’

shell scripts sample

#!/bin/bash for ((i=1;i<=254;i++)) do ip=192.168.1.$i ping $ip -c 1 |grep -q ‘ttl=’ && echo “$ip” >> alive.txt done

#!/bin/bash groupadd websphere for user in app1 app2 app3 app4 do useradd -g websphere -s /sbin/nologin -d /dev/null $user (echo $user;echo $user) | smbpasswd -s -a $user done

cat access.log |awk ‘{print $1}’|sort |uniq -c |sort -nr […]

shell bash guide for reference

Download (PDF, 3.17MB)

bash reference

Shell program

View System Date, Calender

Calender

[root@cluster1 ~]# cal April 2015 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

[root@cluster1 ~]# cal 7 2015 July […]

SHELL SCRIPTING Reference

SHELL SCRIPTING:

-shell is a command line interpreter

-it is interface b/w user & kernel

– it takes commands form the user and excute them

Shell  are different types:

Shell           developed   shellprompt                   executetioncommand

Bourne        SteveBorn   $                           sh

Corn           devidCorn   $                           ksh

Cshell                   billjoy                   %                          csh

Bash           born            $                           sh(or)bsh

Zsh             panel           $                           zsh

[…]

tee command and examples

NAME tee – read from standard input and write to standard output and files

SYNOPSIS tee [OPTION]… [FILE]…

DESCRIPTION Copy standard input to each FILE, and also to standard output. -a, –append append to the given FILEs, do not overwrite -i, –ignore-interrupts ignore interrupt signals –help display this help and exit –version output version information […]

Page 1 of 712345...Last »