November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

SED

FILE SPACING: # double space a file sed G # double space a file which already has blank lines in it. Output file # should contain no more than one blank line between lines of text. sed ‘/^$/d;G’ # triple space a file sed ‘G;G’ # undo double-spacing (assumes even-numbered lines are always blank) sed […]

Format date for display or to use in a shell script

Q. How do I format date to display on screen on for my scripts as per my requirements? A. You need to use standard date command to format date or time for output or to use in a shell script.

 

Syntax to specify formatdate +FORMAT Task: Display date in mm-dd-yy formatType the command as […]

UNIX Bourne Shell Scripting

UNIX Bourne Shell Scripting

These are the workshop notes I used to use for teaching a course on Bourne shell (sh) scripting. I haven’t taught the class for some time now, but the material is all still current since the Bourne shell hasn’t changed. Often you’ll find that these notes are a bit short of […]

UNIX Shell Scripting

UNIX Shell Scripting

The basic concept of a shell script is a list of commands, which are listed in the order of execution. A good shell script will have comments, preceded by a pound sign, #, describing the steps. There are conditional tests, such as value A is greater than value B, loops allowing us […]

LOG using Shell script

#!/bin/sh LOGFILE=/tmp/apache.log SCRIPT_DIR=~/bin

logmsg () { echo $(date): $1 >> $LOGFILE }

while read instance do if [ -x “${SCRIPT_DIR}/$instance” ] ; then logmsg “Recycle $instance…” “${SCRIPT_DIR}/$instance” start >> $LOGFILE 2>&1 else logmsg “Cannot find recycle script for $instance – not starting” fi done < “${SCRIPT_DIR}/active-servers” logmsg “Start script execution complete”

exit status code

How to check the exit status code

When a command finishes execution, it returns an exit code. The exit code is not displayed on the screen by default. To examine the exit code, you need to examine a special variable, “$?”

Say, you are searching for a string in a text file.

$ grep x1y2z3 […]

check if folder exists

Bash Script check if folder exists

This piece of snippet code below will check if a folder exists.

Linux OS that has NTFS driver installed, can easily to plug in and plug out NTFS formatted external drives.

Or look for ntfs-3g driver if NTFS is not supported.

And can easily be used by both Windows […]

HOW TO SEND MAIL FROM SHELL SCRIPT

! /bin/bash #Use: To Send Mail From Shell Script in Linux #Prepared By: Online cyber Study Group TO_ADDRESS=”testusr@yourdomain.com” FROM_ADDRESS=”root@ yourdomain.com” SUBJECT=”HOW TO SEND MAIL FROM SHELL SCRIPT” BODY=” This is free webmail server we also called it open source mail server and we have write a bash script that have title HOW TO SEND MAIL […]

Shell script for apache dos protection

#!/bin/bash cur=`date +%H%M%S` becur=`date -d “1 minute ago” +%H%M%S` badip=`tail -n 10000 /var/log/httpd/access_log | egrep -v “\.(gif|jpg|jpeg|png|css|js)” | awk -v a=”$becu r” -v b=”$cur” -F [‘ ‘:] ‘{t=$5$6$7;if (t>=a && t=20) print $2} ‘` if [ ! -z $badip ];then for ip in $badip; do if test -z “`/sbin/iptables -nL | grep $ip`”;then /sbin/iptables -I […]

Shell script to get the user list

#!/bin/bash awk -F: ‘{ print $1 }’ /etc/passwd awk -F: ‘{ print $1,$5 }’ /etc/passwd awk -F: ‘{ print $1,$NF }’ /etc/passwd awk ‘NF >0’ awk -F: -v ‘OFS=—‘ ‘{ print $1,$5 }’ /etc/passwd exit 0