PDA

View Full Version : Shell Script



tux-world
پنج شنبه 07 دی 1385, 16:51 عصر
سلام
دوتا اسکریپت داشتم تو تمپ هام حیفم اومد نذارمش سایت

یکیش نمایش ترافیک شبکه هستش


options:
<n>s|m|h|d, n is an integer referring to time interval in
seconds|minutes|hours|days which refers to the frequency of how
often to display traffic info
<interface>, is the name of network interface as found in /proc/net/dev file
-h, usage and options (this help)
-m, manual
-l, see this script"


#! /bin/sh
# ################################################## ###########################

NAME_="bytetraf"
HTML_="monitor network traffic"
PURPOSE_="display network traffic on a interface"
SYNOPSIS_="$NAME_ [-hml] <n>s|m|h|d <interface>"
REQUIRES_="standard GNU commands, /proc fs"
VERSION_="1.0"
DATE_="2004-07-20; last update: 2004-12-16"
AUTHOR_="Dawid Michalczyk <dm@eonworks.com>"
URL_="www.comp.eonworks.com"
CATEGORY_="net"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# ################################################## ###########################
# This program is distributed under the terms of the GNU General Public License

usage () {

echo >&2 "$NAME_ $VERSION_ - $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
<n>s|m|h|d, n is an integer referring to time interval in
seconds|minutes|hours|days which refers to the frequency of how
often to display traffic info
<interface>, is the name of network interface as found in /proc/net/dev file
-h, usage and options (this help)
-m, manual
-l, see this script"
exit 1
}

manual() { echo >&2 "

NAME

$NAME_ $VERSION_ - $PURPOSE_

SYNOPSIS

$SYNOPSIS_

DESCRIPTION

Output format example:

13:53:21 int: ppp0 recv: [+6270] 5.789.621 tran: [+393] 537.478 rate: 6663 b/s

Output explanation for each column:

13:53:21 - time
int: - short for interface
ppp0 - interface type
recv: - marks received bytes start
[+6270] - received 6270 bytes since last time interval specified as <n>s|m|h|d
5.789.621 - received 5.789.621 bytes since the interface has been up
tran: - marks transferred bytes start
[+393] - transferred 393 bytes since last time interval specified as <n>s|m|h|d
537.478 - transferred 537.478 bytes since the interface has been up
rate: - speed in bytes/s of the total traffic
6663 b/s - 6663 bytes/s

NOTE

This script relies on the structure of the output generated by kernels 2.2.x and
2.4.x in /proc/net/dev file.

AUTHOR

$AUTHOR_ Suggestions and bug reports are welcome.
For updates and more scripts visit $URL_

"; exit 1; }

# define net interface
interface=$2

# local funcs
byte_count() {

while read line;do

if [[ $line == ${interface}:* ]];then
[[ $1 == r ]] && { set -- ${line#*:}; echo $1; }
[[ $1 == t ]] && { set -- ${line#*:}; echo $9; }
fi

done < /proc/net/dev
}

string_intDelim() {
echo $1 | sed '{ s/$/@/; : loop; s/\(...\)@/@.\1/; t loop; s/@//; s/^\.//; }'
}

interface_check() {
grep $interface /proc/net/dev &>/dev/null
[ $? != 0 ] && { echo >&2 $interface is not up, cant find it in /proc/net/dev; exit 1; }
}

# args check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# enabling extended globbing
shopt -s extglob

# arg handling and execution
case $1 in

-h) usage ;;
-m) manual ;;
-l) more $0; exit 1 ;;
+([0-9])[smhd] ) # main execution

[ $2 ] || { echo >&2 missing second argument, type $NAME_ -h for help; exit 1; }
interface_check

intg=${1%[s|m|h|d]*}
unit=${1##*[!s|m|h|d]}

[[ $unit == s ]] && div=1
[[ $unit == m ]] && div=60
[[ $unit == h ]] && div=3600
[[ $unit == d ]] && div=86400

while grep $interface < /proc/net/dev &>/dev/null ;do

received=$(byte_count r)
transmit=$(byte_count t)
sleep $1 # time interval for how often to grab data from /proc/net/dev
nreceived=$(byte_count r)
ntransmit=$(byte_count t)
rdiff=$((( $nreceived - $received )))
tdiff=$((( $ntransmit - $transmit )))
rate=$((( ($rdiff + $tdiff) / ( $div * $intg ) )))
echo "$(date +%H:%M:%S) \
int: $interface \
recv: [+${rdiff}] $(string_intDelim $received) \
tran: [+${tdiff}] $(string_intDelim $transmit) \
rate: ${rate} b/s"

done ;;

*) echo invalid or missing argument, type $NAME_ -h for help ; exit 1 ;;
esacدومیش هم :
تبدیل فایل pdf به تکست Ascii هستش (انگلیسی)

#! /bin/sh
# ################################################## ###########################

NAME_="pdf2txt"
HTML_="convert pdf to text"
PURPOSE_="convert pdf file to ascii text; write the converted file to disk"
SYNOPSIS_="$NAME_ [-vhlr] <file> [file...]"
REQUIRES_="standard GNU commands, ps2ascii"
VERSION_="1.0"
DATE_="2004-04-18; last update: 2005-03-03"
AUTHOR_="Dawid Michalczyk <dm@eonworks.com>"
URL_="www.comp.eonworks.com"
CATEGORY_="text"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# ################################################## ###########################
# This program is distributed under the terms of the GNU General Public License

usage () {

echo >&2 "$NAME_ $VERSION_ - $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
-r, remove input file after conversion
-v, verbose
-h, usage and options (help)
-l, see this script"
exit 1
}

# arg check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# var initializing
rmf=
verbose=

# option and argument handling
while getopts vhlr options; do

case $options in
r) rmf=on ;;
v) verbose=on ;;
h) usage ;;
l) more $0 ;;
\?) echo invalid or missing argument, type $NAME_ -h for help; exit 1 ;;
esac

done

shift $(( $OPTIND - 1 ))

# check if required command is in $PATH variable
which ps2ascii &> /dev/null
[[ $? != 0 ]] && { echo >&2 the required \"ps2ascii\" command is not in your PATH; exit 1; }

# main
for a in $@; do

if [ -f ${a%.*}.txt ]; then
echo ${NAME_}: skipping: ${a%.*}.txt file already exist
continue
else
[[ $verbose ]] && echo "${NAME_}: converting: $a -> ${a%.*}.txt"
ps2ascii $a > ${a%.*}.txt
[[ $? == 0 ]] && stat=0 || stat=1
[[ $stat == 0 ]] && [[ $verbose ]] && [[ $rmf ]] && echo ${NAME_}: removing: $a
[[ $stat == 0 ]] && [[ $rmf ]] && rm -f -- $a
fi

done

tux-world
پنج شنبه 07 دی 1385, 20:33 عصر
این اسکریپت جالب هم کارش این هستش که یه برنامه رو تو یه زمان خاص از حافظه بیرون میاره یا به اصطلاح kill می کنه و بسیار جالب هستش
این هم کدش : :گیج:



requires: standard GNU commands

version: 1.0

usage: timelykill [-hl] <pid> <n>s|m|h|d

options:
<pid> <n>s|m|h|d, pid and n seconds|minutes|hours|days to wait before killing the process
-h, usage and options (this help)
-l, see this script"



#! /bin/sh
# ################################################## ###########################

NAME_="timelykill"
HTML_="process pid"
PURPOSE_="kill process pid after n time"
SYNOPSIS_="$NAME_ [-hl] <pid> <n>s|m|h|d"
REQUIRES_="standard GNU commands"
VERSION_="1.0"
DATE_="2004-06-20; last update: 2005-02-28"
AUTHOR_="Dawid Michalczyk <dm@eonworks.com>"
URL_="www.comp.eonworks.com"
CATEGORY_="sys"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# ################################################## ###########################
# This program is distributed under the terms of the GNU General Public License

usage () {

echo >&2 "$NAME_ $VERSION_ - $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
<pid> <n>s|m|h|d, pid and n seconds|minutes|hours|days to wait before killing the process
-h, usage and options (this help)
-l, see this script"
exit 1
}

# enabling extended globbing
shopt -s extglob

# local func
check_pid() {

kill -0 $1 &>/dev/null # using bash kill built in
[ $? = 0 ] && return 0 || return 1

}

time_validateSleepArg() {

case $1 in
+([0-9])[smhd] ) return 0 ;;
*) return 1 ;;
esac
}

# option handling
case $1 in
-h) usage ;;
-l) more $0; exit 1 ;;
+([0-9])) # arg1 can only be an integer

# sleep arg check
time_validateSleepArg $2
[ $? = 0 ] || { echo >&2 second argument invalid, type $NAME_ -h for help; exit 1; }

check_pid $1 ; [ $? != 0 ] && { echo >&2 pid $1 does not exits; exit 1; }
sleep ${2} # before the kill

while kill -0 $1 &>/dev/null ;do

kill -15 $1; check_pid; [ $? == 1 ] && exit 0
kill -3 $1; check_pid; [ $? == 1 ] && exit 0
kill -9 $1; check_pid; [ $? == 1 ] && exit 0
echo process can not be killed, possibly due to lack of ownership rights
exit 1

done ;;

*) echo invalid or missing argument, type $NAME_ -h for help ; exit 1 ;;

esac

tux-world
جمعه 08 دی 1385, 10:44 صبح
این دو تا رو هم داشته باشین که بسیار کاربردی هستن
اولی اینکه تمام فایلهای فشرده رو باز می کنه و سختی کار رو برای کاربرای مبتدی از بین می بره
فایلهای فشرده ای رو که باز می کنه : zip, tar, tgz, tar.gz, tar.bz2, tar.z


requires: standard GNU commands
version: 1.2
usage: unpack2dir [-vhlr] <file> [file...]
options:


-r, remove the compressed file after extraction
-v, verbose
-h, usage and options (help)
-l, see this script"


#! /bin/sh
# ################################################## ###########################

NAME_="unpack2dir"
HTML_="uncompress unpack compressed files"
PURPOSE_="unpack zip, tar, tgz, tar.gz, tar.bz2, tar.z to a dir of the same name as archive prefix"
SYNOPSIS_="$NAME_ [-vhlr] <file> [file...]"
REQUIRES_="standard GNU commands"
VERSION_="1.2"
DATE_="1999-09-20; last update: 2006-02-03"
AUTHOR_="Dawid Michalczyk <dm@eonworks.com>"
URL_="www.comp.eonworks.com"
CATEGORY_="compress"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# ################################################## ###########################
# This program is distributed under the terms of the GNU General Public License

# HISTORY:
# 2006-02-03 v1.2 - added the -C flag to tar options. Otherwise tar would not
# extract to a dir with different name then the one found in the
# archive.

usage () {

echo >&2 "$NAME_ $VERSION_ - $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
-r, remove the compressed file after extraction
-v, verbose
-h, usage and options (help)
-l, see this script"
exit 1
}

# args check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# var init
rmf=
verbose=

# option and argument handling
while getopts vhlr options; do

case $options in
r) rmf=on ;;
v) verbose=on ;;
h) usage ;;
l) more $0; exit 1 ;;
\?) echo invalid argument, type $NAME_ -h for help; exit 1 ;;
esac

done
shift $(( $OPTIND - 1 ))

mkdirf() {

# usage: fnc <file_prefix> <file>

[ -d $1 ] && { echo "${NAME_}: skipping ${2} - dir ${1} already exist" ; continue; }
#echo $1
mkdir $1
# [[ $verbose ]] && echo "${NAME_}: unpacking "$2
}

file_getDirname() {

local _dir="${1%${1##*/}}"
[ "${_dir:=./}" != "/" ] && _dir="${_dir%?}"
echo "$_dir"

}

file_getBasename() {

local _name="${1##*/}"
echo "${_name%$2}"

}

clean() {

# usage <exit_status> <dir_to_rm>

[[ $1 != 0 ]] && rmdir $2 # remove empty dir if unpacking went wrong
[[ $1 == 0 && $verbose ]] && echo "${NAME_}: unpacking " ${dir}/${a}
[[ $rmf ]] && rm -f -- $a

}

start_dir=$(pwd)

for a in "$@"; do

cd $start_dir
fname=$(file_getBasename $a)
dir=$(file_getDirname $a)
cd $dir
a=$fname

case $a in


# zip
*.[zZ][iI][pP])
mkdirf ${a/.[zZ][iI][pP]/} $a
unzip -qq $a -d ${a/.[zZ][iI][pP]/}
clean $? ${a/.[zZ][iI][pP]/}
;;

# tar
*.[tT][aA][rR])
mkdirf ${a/.[tT][aA][rR]/} $a
tar -xf $a -C ${a/.[tT][aA][rR]/}/
clean $? ${a/.[tT][aA][rR]/}
;;

# tgz
*.[tT][gG][zZ])
mkdirf ${a/.[tT][gG][zZ]/} $a
tar -xzf $a -C ${a/.[tT][gG][zZ]/}
clean $? ${a/.[tT][gG][zZ]/}
;;

# tar.gz
*.[tT][aA][rR].[gG][zZ])
mkdirf ${a/.[tT][aA][rR].[gG][zZ]/} $a
tar -xzf $a -C ${a/.[tT][aA][rR].[gG][zZ]/}/
clean $? ${a/.[tT][aA][rR].[gG][zZ]/}
;;

# tar.bz2
*.[tT][aA][rR].[bB][zZ]2)
mkdirf ${a/.[tT][aA][rR].[bB][zZ]2/} $a
tar -xjf $a -C ${a/.[tT][aA][rR].[bB][zZ]2/}/
clean $? ${a/.[tT][aA][rR].[bB][zZ]2/}
;;

# tar.z
*.[tT][aA][rR].[zZ])
mkdirf ${a/.[tT][aA][rR].[zZ]/} $a
tar -xZf $a -C ${a/.[tT][aA][rR].[zZ]/}/
clean $? ${a/.[tT][aA][rR].[zZ]/}
;;


*) echo "${NAME_}: $a not a compressed file or lacks proper suffix" ;;

esac

done
این برنامه هم فایلهای Msword رو تو حالت متنی نمایش میده و برای زمانی که کاربرا از x-window استفاده نمی کنن (مخصوصا حرفه ای ها) بسیار پر کاربرد هستش و نشون میده که با چند تا دستور خاص میشه اونها رو به راحتی مطالعه کرد


purpose: display ms word doc file in ascii format
requires: standard GNU commands, catdoc
version: 1.0
usage: word2txt [-hl] <doc_file> [doc_file...]
options:


-h, usage and options (this help)
-l, see this script"


این هم کد اسکریپت :

#! /bin/sh
# ################################################## ###########################

NAME_="word2txt"
HTML_="convert word to text"
PURPOSE_="display ms word doc file in ascii format"
SYNOPSIS_="$NAME_ [-hl] <doc_file> [doc_file...]"
REQUIRES_="standard GNU commands, catdoc"
VERSION_="1.0"
DATE_="2004-07-22; last update: 2004-07-22"
AUTHOR_="Dawid Michalczyk <dm@eonworks.com>"
URL_="www.comp.eonworks.com"
CATEGORY_="text"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# ################################################## ###########################
# This program is distributed under the terms of the GNU General Public License

usage () {

echo >&2 "$NAME_ $VERSION_ - $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
-h, usage and options (this help)
-l, see this script"
exit 1
}

# missing args check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# arg handling and main script execution
case $1 in

-h) usage ;;
-l) more $0; exit 1 ;;
*) # main script execution

# check if required command is in $PATH variable
which catdoc &> /dev/null
[[ $? != 0 ]] && { echo >&2 the needed \"catdoc\" command is not in your PATH; exit 1; }

for a in $@;do
catdoc -b -s cp1252 -d 8859-1 -a $1
done ;;

esac

من خودم خیلی از این نوع اسکریپتها استفاده می کنم چون اجرا و کاربرد اونها بسیاری مواقع برام از محیط گرافیکی آسونتو و کاربردی تر هستش . موفق باشید

oxygenws
جمعه 08 دی 1385, 18:28 عصر
کد های تک خطی:
تغییر دسترسی تمام فایل های همین شاخه به ۶۴۴:


find . -type f -exec chmod 644 {} \;


تغییر دسترسی تمام شاخه ها به ۷۵۵:


find . -type d -exec chmod 755 {} \;

tux-world
جمعه 08 دی 1385, 19:09 عصر
سلام خوب این هم اسکریپتهای جالب دیگه :گیج:
برنامه ای که قادره فایلهای صوتی رو اون هم به صورت تصادفی پخش کنه :لبخند:


#!/bin/sh
#random-sound.sh: play a random file from the sounds directory
cd $HOME/sounds
filename=$(find -type f |
awk 'BEGIN{
srand()
}
{
names[NR]=$0
}
END{
i=1+int(rand()*NR)
print names[i]
}
')
echo "$filename"
wavplay "$filename"
اسکریپت بعدی قادره فایلهای jpg تصاعدی رو به فایل عکس نرمال تبدیل کنه

#!/bin/sh
#fixjpg: convert "Progressive" jpg files to normal jpg
for i in "$@"
do
if djpeg < "$i" | cjpeg > "$i.new"
then
mv "$i.new" "$i"
else
echo "fixjpg: failed on $i"
rm "$i.new"
fi
done

tux-world
جمعه 08 دی 1385, 19:19 عصر
اسکریپتی که قادره آی پی فعلی رو برگردونه


#!/bin/sh
ifconfig ippp0 |
sed -n 's/.*inet addr:\([0-9.]*\).*/\1/p'

اسکریپتی که قادره یه ftp رو براتون شبیه سازی کنه :



#! /bin/sh
# ftpfile: get a file ($2) from an ftp site ($1), or interpret a url
# to do the same
PATH=$PATH:/usr/local/bin

progname=`basename $0`
url=0
case $# in
0) 1>&2 echo "$progname: usage $progname ftpsite file or $progname url"; exit 1;;
esac

url=`echo $1|sed 's/\(ftp:\/\/\).*/\1/'`
case $url in
"ftp://") isurl=1 ;;
"*") 1>&2 echo "$progname: usage $progname ftpsite file"; exit 1;;
esac
if [ "$isurl" -gt 0 ]
then
SOURCE=`echo $1|sed 's/ftp:\/\/\([^/]*\)\/.*/\1/'`
FILE=`echo $1|sed 's/ftp:\/\/\([^/]*\)\/\(.*\)/\2/'`
else
SOURCE=$1
FILE=$2
fi

case $USER in
"") USER='paul' ;;
esac

BFILE=`basename $FILE`
if [ -f /tmp/$FILE ]
then
op='reget'
else
op='get'
fi

case $SOURCE in
ftp.demon.co.uk) ftpuser='hostname'; ftppassword='password' ;;
*) ftpuser='anonymous'; ftppassword="$USER@" ;;
esac

ftp -n $SOURCE <<EndFTP
user $ftpuser $ftppassword
binary
$op $FILE /tmp/$BFILE
EndFTP

این هم اسکریپت بی دردسر برای mailbox_POP3


#!/bin/sh
#chkpop: check for mail in a pop3 mailbox
case $1 in
"")
1>&2 echo "chkpop: chkpop <server>"
exit 2
;;
esac
line=$(awk '/'$1'/ {print $2, $4, $6}' < $HOME/.netrc)
if [ "$line" = "" ]
then
exit 0
fi
set x $(echo $line)
server=$2
login=$3
password=$4
(echo user $login;echo pass $password;echo quit) |
nc $server 110 |
tee /tmp/chkpop.server_responses |
awk '
/\+OK .*([0-9][0-9]*).*messages.*/{
if (match($0,/[1-9]/))
{print "you have mail on '$server'"; exit 0}
else
{exit 1}
}
'
exit $?

فعلا اینا رو داشته باشین بیش از 4 گیگ template دارم رو هارد اون هم قرو قاتی :گیج:
موفق باشید