Thanks to antville.org
shell script example

An example of shell script.
This will print numbers 0 to 500 in one line.

!/bin/bash

Z=0
LIMIT=500
while [ "$Z" -lt "$LIMIT" ]

do
echo -n "$Z... "
Z=$( expr $Z + 1 )
done
echo
exit 0


ping the address received from positional parameter

!/bin/bash

if [ $# -eq 0 ]
then
SITE="google.com"
else
SITE="$1"
fi

ping -c 2 $SITE > /dev/null
if [ $? != 0 ]
then
echo $(date +%F) ... Network Failure!
fi


Here is the script that will loop two thousand times and print the statement on the screen.

!/bin/sh

MAX=2000
i=1
echo " truncate table t1; "

while [ $i -le $MAX ]
do
echo " insert into t1(f1) values($i); "
i=expr $i + 1
done

... Link (0 comments) ... Comment


Match Operator in AWK

The match operator is the tilde “~” which will match an expression in a record or field. In this example awk is used to search for a field, number 7, that matches “nologin”.

awk -F: '$7 ~ /nologin/' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

... Link (0 comments) ... Comment


Copy table

!/bin/sh

for i in find /var/lib/mysql/test/ -name "Table1.*"
do
cp $i dirname $i'/test'basename $i
done

duplicate a table Table1 by coping the files if it is a MyISAM table.

... Link (0 comments) ... Comment


Inernal File Separator

The safe way to change IFS in a 'while read' loop is:

while IFS=| read a b c
do
: whatever
done

... Link (0 comments) ... Comment


valid date

validdate.sh script will look like this...

!/bin/sh

userdate=$1
date -d $userdate &> /dev/null
if [ $? -ne 0 ] ; then
echo You entered an invalid date, $userdate
else
echo The date $userdate is valid
fi

sh validdate.sh 2009-33-44

You entered an invalid date, 2009-33-44

... Link (0 comments) ... Comment


Backup a directory

!/bin/sh

tar czvf $1.$(date +%Y%m%d-%H%M%S).tgz $1
exit $?

The arc script accepts a single file or directory name as a parameter and creates a compressed archive file with the current date embedded into the resulting archive file's name.

... Link (0 comments) ... Comment


better code

test -s /var/log/mailme.txt
if [[ $? -eq 0 ]] ; then
The above 2 lines can be written in one line as shown below:

if [[ -z /var/log/mailme.txt ]]; then

... Link (0 comments) ... Comment


read .csv file

!/bin/sh

awk -F"," '{print $1,$2}' read1.txt | while read var1 var2
do
echo $var1 $var2
done

... Link (0 comments) ... Comment


findtable shell script

vi /bin/findtable

!bin/sh

mysql -e"select TABLE_SCHEMA, TABLE_NAME, ENGINE, TABLE_ROWS, CREATE_TIME FROM information_schema.TABLES where TABLE_NAME LIKE '%$1%';"

chmod 777 /bin/findtable

sh findtable test

... Link (0 comments) ... Comment


Online for 6205 days
Last modified: 8/7/19 10:49 AM
Status
Youre not logged in ... Login
Menu
... Home
... Tags

Search
Calendar
August 2019
SunMonTueWedThuFriSat
123
45678910
11121314151617
18192021222324
25262728293031
June
Recent updates
Remove old backup files Cron
entry to remove all files in /home/ folder that starts...
by shantanuo (8/7/19 10:49 AM)
Types in AWS CNF List
of important Types in Amazon Cloud Formation tool: IAM (User,...
by shantanuo (8/5/19 7:36 AM)
codes used for classification Standard
codes used for classification: 1 SC 2 OBC 3 SBC...
by shantanuo (6/25/19 11:11 AM)
Shell scripting basics Here are
8 important concepts of Shell scripting that everyone should know....
by shantanuo (6/2/19 6:50 AM)
schema conversion tool This seems
to be a useful tool to convert mysql or oracle...
by shantanuo (3/30/19 6:52 AM)
Install packetbeat In order to
install packetbeat, you may need to change the windows security...
by shantanuo (3/26/19 8:01 AM)
python itertools module from itertools
import groupby s = 'abcccccbba' for w, v in groupby(s):...
by shantanuo (3/17/19 3:22 PM)
audio visual modules for Machine
Learning Here is the list of some of the modules...
by shantanuo (3/16/19 1:08 PM)
google module module for google
search !pip install google from googlesearch import search for url...
by shantanuo (1/8/19 10:38 AM)
Python date and time from
datetime import datetime date_string = "21 June, 2018" date_object =...
by shantanuo (12/26/18 12:14 PM)
Infer types in pandas This
will return the mixed datatypes but this can be slow....
by shantanuo (12/16/18 3:50 PM)
Extract words from Marathi wikipedia
1) Download the polyglot pickled marathi model wget http://bit.ly/12FI1xV mv...
by shantanuo (11/4/18 10:06 AM)
Google Input tools Google suddenly
stopped download of Input Tools package without giving any reason....
by shantanuo (9/10/18 7:44 AM)
Python objects and methods Here
are important python objects and their methods int __mul__, __add__,...
by shantanuo (8/18/18 3:11 PM)
JSON to DF Here is
how to read a JSON file into pandas dataframe. import...
by shantanuo (8/8/18 1:12 PM)
managing servers using python Fabric
is a high level Python library designed to execute shell...
by shantanuo (8/4/18 10:01 AM)
backup mysql users It is
easy to backup mysql users - wget percona.com/get/pt-show-grants; perl pt-show-grants
by shantanuo (5/20/18 3:42 PM)
About python 1 everything is
an object (class instances) 2 methods depends upon class of...
by shantanuo (5/20/18 2:52 PM)
mysql and shell command date
-d"-1 day ago" +'%b%d' equals to mysql -Bse"select date_format(date(date_add(now() ,...
by shantanuo (5/16/18 7:47 AM)
Preserving comments in Stored Procedure
You can write multi line comments in a stored procedure...
by shantanuo (5/12/18 12:56 PM)
Custom slack command Here are
5 steps to add any API to your slack channel....
by shantanuo (4/25/18 6:59 AM)
stack bookmarklet This bookmarklet will
help to quickly access stack pages linking to current page...
by shantanuo (1/10/18 10:47 AM)
Manage S3 like rsync ####
create bucket aws s3 mb s3://johntest123/ #### rsync all files...
by shantanuo (12/22/17 12:04 PM)
apache docker container issues If
apache docker container does not start then make these changes......
by shantanuo (11/10/17 3:00 PM)
Install aws client on centos
If pip is not installed, then the following command will...
by shantanuo (10/26/17 9:51 AM)

RSS feed

Made with Antville
Helma Object Publisher