1. 写一个脚本,利用循环计算10的阶乘#!/bin/sh
factorial=1
for a in `seq 1 10`
do
       factorial=`expr $factorial \* $a`
done
echo "10! = $factorial"

2. 写一个脚本,执行后,打印一行提示“Please input a number:",要求用户输入数值,然后打印出该数值,然后再次要求用户输入数值。直到用户输入"end"停止。#!/bin/sh
unset var

while [ "$var" != "end" ]
do
     echo -n "please input a number: "
     read var
     if [ "$var" = "end" ]
     then
         break
     fi
     echo "var is $var"
done
//////////////////////////////////////////
#!/bin/sh
unset var
while [ "$var" != "end" ]
do
     echo -n "please input a number: "
     read var
     if [ "$var" = "end" ]
     then
         break
     fi
     echo "var is $var"
done

3. 写一个脚本,利用循环和continue关键字,计算100以内能被3整除的数之和

4.一个函数,利用shift计算所有参数乘积,假设参数均为整数

#! /bin/sh
result=1
while [ $# -gt 0 ]
do
     result=`expr $result * $1`
     shift
done
echo $result

5.写一个脚本,可以根据参数文件名,以正确的参数调用tar来解压缩tar.gz或tar.bz2文件。#!/bin/shcase ${1##*.tar.} in     bz2)         tar jxvf $1         ;;     gz)         tar zxvf $1         ;;     *)         echo "wrong file type"esac6.写一个脚本以方便用户查询rpm的相关信息。这个脚本首先提示用户选择查询依据,比如文件名,包名,全部等。然后提示用户选择查询信息,比如包名,包里所包含的所有文件,包的信息等。然后询问是否继续查询,是则循环刚才的过程,否则退出。

#!/bin/bash
# favourite OS.       samli          2004.4.19
echo "What is your favourite OS?"
select var in "Linux" "UNIX" "Windows" "Other"; do
echo "You have selected $var."
#break
done

# !/bin/bash
# list a content summary of a number of RPM packages           samli         2004.4.19
# USAGE: showrpm rpmfile1 rpmfile2 ...
# EXAMPLE: showrpm /cdrom/Thizlinux/RPMS/*.rpm

for rpmpackage in $*;
do
if [ -r "$rpmpackage" ];
then
echo "=============== $rpmpackage =============="
/bin/rpm -qip $rpmpackage
else
echo "ERROR: cannot read file $rpmpackage"
fi
done

#!/bin/bash
if [ $# -le 0 ]
then
echo "Not enough parameters"
exit 1
fi
#string="2 3 4 5 6"
#set string
sum=0
while [ $# -gt 0 ]
do
sum=`expr $sum + $1`
shift
done
echo $sum

#! /bin/bash
echo "*******************************"
echo "Please select your operation:"
echo " 1 Copy"
echo " 2 Delete"
echo " 3 Backup"
echo "*******************************"
read op
case $op in
     C)
       echo "your selection is Copy"
     ;;
     D)
       echo "your selection is Delete"
     ;;
     B)
       echo "your selection is Backup"
     ;;
     *)
       echo "invalid selection"
esac
#! /bin/sh
while true
do
   echo "*******************************"
   echo "Please select your operation:"
   echo " 1 Copy"
   echo " 2 Delete"
   echo " 3 Backup"
   echo " 4 Quit"
   echo "*******************************"
   read op
   case $op in
        C)
          echo "your selection is Copy"
           ;;
        D)
          echo "your selection is Delete"
          ;;
        B)
         echo "your selection is Backup"
           ;;
        Q)
          echo "Exit ..."
               break
          ;;
        *)
          echo "invalid selection,please try again"
   esac
done
#! /bin/sh
a=10
b=20
c=30
value1=`expr $a + $b + $c`
echo "The value of value1 is $value1"
value2=`expr $c / $b`
echo "The value of value2 is $value2"
value3=`expr $c * $b`
echo "The value of value3 is $value3"
value4=`expr $a + $c / $b`
echo "The value of value4 is $value4"
var4=`expr $value4 - $value2`
echo $var4

#! /bin/sh
sum=0
for i in $* 
do
    sum=`expr $sum + $i`
done
echo $sum
abc=123
echo $abc

###定义函数

example1()

{

abc=456

}

###调用函数

example1

echo $abc

abc=234234

example1

echo $abc

###定义函数,使用参数

example2()

{

echo $1

echo $2

}

###调用函数,向它传递参数

example2 abc bbb

example2 dksdfsdfsfaa bbb

#!/bin/bash
echo "please input a file name:"
read file_name
if [ -d $file_name ]
then
echo "$file_name is a directory"
elif [ -f $file_name ]
then 
echo "$file_name is a regular file"
elif [ -c $file_name -o -b $file_name ]
then 
echo "$file_name is a device file"
else
echo "$file_name is an unkonwn file"
fi

#! /bin/sh
read first second third
echo "the first parameter is $first"
echo "the second parameter is $second"
echo "the third parameter is $third"

#! /bin/sh
if [ $# -ne 2 ]; then
   echo "Not enough parameters"
   exit 0
fi

if [ $1 -eq $2 ]; then
   echo "$1 equals $2"
elif [ $1 -lt $2 ]; then
   echo "$1 less than $2"
elif [ $1 -gt $2 ]; then
   echo "$1 greater than $2"
fi

#clear
#:trap "" 2 18 20 24
#e cho "***********************************************************************"
#e cho " HI! You changed my root password"
#e cho " This is a little punishment for you then you can use the shell"
#e cho " You must answer my three questions and type "yes" or "no" to answer"
#
#answer="no"
#
#e cho "***********************************************************************"
#e cho "Please press "Enter" key to continue"
#read
#while [ $answer = "no" ]
#do
#     echo
#     echo "1.Are you a boy ? (yes/no)"
#     read answer
#done
#
#answer="no"
#
#until [ $answer = "yes" ]
#do
#     echo
#     echo "2.Are you foolish ? (yes/no)"
#     read answer
#done
#
#answer="no"
#
#until [ $answer = "yes" ]
#do
#     echo
#     echo "3.Do you love me ? (yes/no)"
#     read answer
#done
#
#e cho "***********************************************************************"
#e cho
#e cho "Now ! I know you are a foolish girl and you love me."
#e cho
#e cho "So you can continue"
#e cho
#e cho "**************************************************************"
#e cho
#sleep 3
#e cho "    (haha ! It is just a joke)"
i=1
echo "this time i is not equal to 4"
while [ $i -le 8 ]
do
       ((i=i+1))
       if [ $i -eq 4 ]
       then
              echo "this time i is equal to 4"
              continue
       fi
       echo "this time i is not equal to 4"
done
echo "The command is $0"
echo "The first argument is $1, the second argument is $2"
echo "The entire command is $0 $1 $2"
echo "And there are $# arguments"
echo "The end of testing"

#! /bin/sh
if [ $# -gt 1 ] 
then
echo "Too many parameters"
exit 1
fi

if [ $# -eq 0 ] 
then
echo "Too few parameters"
exit 100
fi

if [ ! -d $1 ]
then
echo "Usage : $0 directory"
exit 1
fi

#for i in $1/*
#do
# if [ -x $i -a ! -d $i ]
#    then
#     ls $i
# fi
#done

#!/bin/sh

RPM=/bin/rpm

option="-q"

while true

do

echo "what to query?"

select var in "All" "file" "package name"

do

case $var in

All)

option=$option"a"

break

;;

file)

echo -n "please input file name: "

option=$option"f"

read argument

break

;;

package\ name)

echo -n "please input package name: "

read argument

break

;;

*)

echo "please choose between 1-3"

;;

esac

done

echo "what do you want to know?"

select var in "location" "info" "package name"

do

case $var in

location)

option=$option"l"

break

;;

info)

option=$option"i"

break

;;

package\ name)

break

;;

*)

echo "please choose between 1-3"

;;

esac

done

${RPM} $option $argument

echo "continue? [yes/no]"

read answer

if [ answer = "no" ]

then

break

fi

done

#!/bin/sh
sum=0
for a in `seq 1 100`
do
     if [ `expr $a % 3` -ne 0 ]
     then
          continue
     fi
     echo $a
     sum=`expr $sum + $a`
done
echo "sum = $sum"
#!/bin/bash
ftype=`file "$1"`
case "$ftype" in
"$1: Zip archive"*)
/usr/bin/unzip "$1" ;;
"$1: gzip compressed"*)
/bin/gunzip "$1" ;;
"$1: bzip2 compressed"*)
/usr/bin/bunzip2 "$1" ;;
*)
echo "Sorry, file $1 can not be uncompressed with smartzip." ;;
esac

注:上面有一行,for a in `seq 1 10`,其中seq 1 10 , 即列出现1到10之间所有的数字,这一行也可改为:for a in "1 2 3 4 5 6 7 8 9 10", 在用 expr 进行乘法运算时,为了避免 * 被看成匹配符,需要对其进行转义, 用 \*

自动备份数据库脚本两个!

MySQL:Linux下自动备份数据库的shell脚本 Linux 服务器上的程序每天都在更新 MySQL 数据库,于是就想起写一个 shell 脚本,结合 crontab,定时备份数据库。其实非常简单,主要就是使用 MySQL 自带的 mysqldump 命令。

脚本内容如下:
#!/bin/sh
# File: /home/mysql/backup.sh
# Database info
DB_NAME="test"
DB_USER="username"
DB_PASS="password"

# Others vars
BIN_DIR="/usr/local/mysql/bin"
BCK_DIR="/home/mysql/backup"
DATE=`date +%F`

# TODO
$BIN_DIR/mysqldump --opt -u$DB_USER -p$DB_PASS $DB_NAME | gzip > $BCK_DIR/db_$DATE.gz

然后使用将此脚本加到 /etc/crontab 定时任务中:

01 5 * * 0 mysql /home/mysql/backup.sh

好了,每周日凌晨 5:01 系统就会自动运行 backup.sh 文件备份 MySQL 数据库了。

/home/www/inc/back

第二个!!!!

      1. #!/bin/sh
      2. # File: /home/mysql/backup.sh
      3. # Database info bakupmysql
      4. DB_USER="user"
      5. DB_PASS="password"
      6. # Others vars
      7. DATE=`date +%Y-%m-%d`
      8. mkdir /home/mysqlbak/$DATE
      9. BIN_DIR="/usr/local/mysql/bin"
      10. BCK_DIR="/home/mysqlbak/$DATE"
      11. # TODO
      12. $BIN_DIR/mysqldump --opt -u$DB_USER -p$DB_PASS discuz > $BCK_DIR/discuz.sql
      13. $BIN_DIR/mysqldump --opt -u$DB_USER -p$DB_PASS zjblog > $BCK_DIR/zjblog.sql
      14. $BIN_DIR/mysqldump --opt -u$DB_USER -p$DB_PASS openads > $BCK_DIR/openads.sql

Linux Shell 脚本的更多相关文章

  1. Linux shell脚本编程(三)

    Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...

  2. Linux shell脚本编程(二)

    Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...

  3. Linux shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  4. Linux Shell 脚本入门

    linux shell 脚本格式 #!/bin/sh#..... (注释)命令...命令... 使用vi 创建完成之后需设置权限 chmod +x filename.sh 执行命令: ./filena ...

  5. Linux Shell脚本入门--cut命令

    Linux Shell脚本入门--cut命令 cut cut 命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields &l ...

  6. Linux Shell脚本攻略 读书笔记

    Linux Shell脚本攻略 读书笔记 这是一本小书,总共253页,但内容却很丰富,书中的示例小巧而实用,对我这样总是在shell门前徘徊的人来说真是如获至宝:最有价值的当属文本处理,对这块我单独整 ...

  7. 阿里Linux Shell脚本面试25个经典问答

    转载: 阿里Linux Shell脚本面试25个经典问答 Q:1 Shell脚本是什么.它是必需的吗? 答:一个Shell脚本是一个文本文件,包含一个或多个命令.作为系统管理员,我们经常需要使用多个命 ...

  8. Linux Shell脚本教程

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  9. Linux shell 脚本攻略之统计文件的行数、单词数和字符数

    摘自:<Linux shell 脚本攻略>

  10. Linux shell 脚本攻略之创建不可修改文件

    摘自:<Linux shell 脚本攻略>

随机推荐

  1. 关于struts2如何去掉默认的后缀(.action)

    struts2是可以配置默认的后缀名的,如http://localhost:8080/test.action,这个是默认的,但是也可以通过配置去修改这个.action为别的. 这里是通过一个常量配置改 ...

  2. jquery 实现层级下拉框联动效果 代码

    <select name="fCareId" id="fCareId"> <option selected="selected&qu ...

  3. 灵魂有香气的女子IOS版本APP,近期将考虑开放源代码

    实在太忙,灵魂有香气的女子这个App,断断续续开发了1个多月了,前后台自己独立完成, 由于接触swift没多久,还属于新手行列,不熟悉,希望大家给出意见, 根据意见,完善后将于近期将考虑开放swift ...

  4. VirtualBox下导入CentOS后,无法上网

        从VirtualBox的"管理"菜单下,选择"导出虚拟电脑",存一个备份.用时,再从"管理"菜单下,选择"导入虚拟电脑&q ...

  5. hdoj 1596 find the safest road

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析:Dijkstra变体,最短路径判断计算方式:Safe(P) = s(e1)*s(e2)…* ...

  6. Linux防火墙(Iptables)的开启与关闭

    Linux防火墙(iptables)的开启与关闭 Linux中的防火墙主要是对iptables的设置和管理. 1. Linux防火墙(Iptables)重启系统生效 开启: chkconfig ipt ...

  7. 0环境设置 - AUTOTRACE设置

    Autotrace是sqlplus的一个工具,用来显示所执行查询的查询计划 设置步骤 • cd [ORACLE_HOME]/rdbms/admin• log into SQL*Plus as SYST ...

  8. mysql 中 isnull 和 ifnull 判断字段是否为null

    对于统计count(type)和avg(type) 都不起作用 SQL中有ISNULL方法,介绍如下: ISNULL使用指定的替换值替换 NULL. 语法ISNULL ( check_expressi ...

  9. Unix环境编程之定时、信号与中断

    在linux下实现精度较高的定时功能,需要用到setitimer 和 getitimer函数. 函数原型: #include <sys/time.h> int getitimer(int ...

  10. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...