April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

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, […]

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

[gview file=”http://rmohan.com/wp-content/uploads/2015/05/unix_tutorial.pdf”]

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

Shell variables:

[…]

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 […]