Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 操作系统 > Linux > linux shell学习笔记 1
【标  题】:linux shell学习笔记 1
【关键字】:linux,shell
【来  源】:http://www.cublog.cn/u/19769/showart.php?id=257061

linux shell学习笔记 1

Your Ad Here

linux shell学习可以告一个段落了。发文庆贺!

下面的脚本在linux as4系统中执行无误。欢迎学习讨论!(此笔记采用两中颜色区分不同的shell脚本)

. echo命令用于显示文本行或变量。

echo [optiong] string

  optiong选项:

  -e解析string 中的转义字符

  -n回车不换行,linux系统默认会车是换行的

string中的转义符 \c,\f,\t,\n\c回车表示会车不换行 \f 禁止,\t相当与tab \n表示会车换行

实例:

#!/bin/bash

#echod

echo -e "this echo's 3 new lines\n\n\n"

echo "ok"

echo

echo "this echo's 3 new lines\n\n\n"

echo "this log files have all been done">mylogfile.txt

.read语句

read语句可以从键盘或文件中的一行文本中读入信息,并将其给一个变量。read varible1 varible2

如果只指定了一个变量,那么read将会把所有的输入赋给此变量,只至遇到第一个文件结束符或会车;如果给出了多个变量,他们按顺序分别被富裕不同的变量。Shell将用空格作为变量间的分割符。

实例:

#!/bin/bash

#readname

echo –n “first name:”

read firstname

echo –n “last name:”

read lastnme

echo –e “your first name is :${firstnaem}\n”

echo –e “your last name is :${lastname}\n”

流控制

   [root@localhost ~]# cat caseselect.txt

#!/bin/bash

#case select

echo -n "Enter a number from 1 to 3:"

read ANS

case $ANS in

1)

 echo "you select 1"

 ;;

2)

 echo "you select 2"

 ;;

3)

 echo "you select 3"

 ;;

*)

 echo "'basename $0': This not between 1 and 3" >&2

 exit;

;;

esac

[root@localhost ~]# cat forlist2.txt

#!/bin/bash

for loop in "orange red blue grey"

do

  echo $loop

done

[root@localhost ~]# cat forlist3.txt

#!/bin/bash

for loop in `cat myfile`

do

  echo $loop

done

 

[root@localhost ~]# cat forloop.txt

#!/bin/bash

for loop in 1 2 3 4 5

do

  echo $loop

done

[root@localhost ~]# cat ifcp.txt

#!/bin/bash

if cp myfile.bak myfile

then

echo "success copy"

else

echo "'basename $0':error could not copy the file">&2

fi

[root@localhost ~]# cat ifelif.txt

#!/bin/bash

#ifelif

echo -n "Enter your name:"

read NAME

if [ -z $NAME ] || [ "$NAME" = "" ];then

        echo "YOU did not enter a name."

elif [ "$NAME" = "root" ];then

        echo "Hello root"

elif [ "$NAME" = "chinaitlab" ];then

        echo "Hello chinaitlab"

else

        echo "You are not root for chinaitlab,but hi,$NAME"

fi

[root@localhost ~]# cat iftest

#!/bin/bash

#if test

if [ "10" -lt "12" ]

then

echo "yes 10 is less than 12"

else

echo "no"

fi

[root@localhost ~]# cat iftest2

#!/bin/bash

#if test2

echo -n "Enter your name:"

read NAME

if [ "$NAME" == "" ];then

echo "You did not enter any information"

else

echo "Your name is ${NAME}"

fi

 

[root@localhost ~]# cat linshi.sh

cat > fileA << "EOF"

this is line 1.

This is line 2.

EOF

[root@localhost ~]# cat name.txt

chinaitlab

shenzhen

[root@localhost ~]# cat names.txt

shenzhen

shanghai

beijing

[root@localhost ~]# cat until_mon

#!/bin/bash

#until_mon

#montior the parttion used

Part="/home"

#get disk used

LOOK_OUT='df|grep "$Part"|awk '{print ${print $5}'|sed 's/%//g''

echo $LOOK_OUT

until [ "$LOOK_OUT" -gt "90" ]

do

        echo "Filesystem /home is nearly full"|mail root

done

[root@localhost ~]# cat until2_mon

#!/bin/bash

#until_mon

#montior the parttion used

Part="/home"

#get disk used

LOOK_OUT='df|grep "$Part"|awk '{print ${print $5}'|sed 's/%//g''

echo $LOOK_OUT

until [ "$LOOK_OUT" -gt "90" ]

do

        echo "Filesystem /home is nearly full"|mail root

        LOOK_OUT='df|grep "$Part"|awk '{print ${print $5}'|sed 's/%//g''

        sleep 3600

done

 

[root@localhost ~]# cat whilebreakout.txt

#!/bin/bash

#breakout

while :

do

        echo -n "Enter any number [1...5]:"

        read ANS

        case $ANS in

        1|2|3|4|5)

                echo "you enter a number between 1 and 5."

                ;;

        *)

                echo "wrong number,Bye."

                break

                ;;

        esac

done

 

[root@localhost ~]# cat whilebreakout2.txt

#!/bin/bash

#breakout

while :

do

        echo -n "Enter any number [1...5]:"

        read ANS

        case $ANS in

        1|2|3|4|5)

                echo "you enter a number between 1 and 5!"

                ;;

        *)

                echo -n "Wrong number,continue (y/n)?:"

                read IS_CONTINUE

                case $IS_CONTINUE in

                 y|yes|Y|Yes)

                        continue

                        ;;

                 *)

                        break

                        ;;

                esac

        esac

done

[root@localhost ~]# cat whileread.txt

echo "press <ctrl>+d to return."

while echo -n "enter the film name you love best:";read FILM

do

        echo "Yeah,${FILM} is a good file!"

done

[root@localhost ~]# cat whileread2.txt

#!/bin/bash

#whileread

while read LINE

do

        echo $LINE

done<names.txt

[root@localhost ~]# cat whileread2.txt

#!/bin/bash

#whileread

while read LINE

do

        echo $LINE

done<names.txt

[root@localhost ~]# cat whileread3.txt

#!/bin/bash

#whileread

while read LINE<names.txt

do

        echo $LINE

done<names.txt

 

SCIM Packages for centos 4.4:【上一篇】
LINUX下可执行文件:【下一篇】
【相关文章】
  • LinuxC 把html转化为js代码
  • Linux平台下建立Oracle透明數據網關,訪問MySQL
  • 在Linux上架構Oracle數據庫系統之安全簡易說明
  • 怎麼樣在 linux 下使用ORACLE 8i 外部例程
  • a good weblog for Oracle base on Linux
  • shell 的两个 "time"
  • linux如何临时增加swap空间(转)
  • linux中让tomcat5自动启动服务
  • 从windows到linux -- 编程篇 -- cygwin,在win中开发linux程序(ZT)
  • Linux C 支持正则表达式的字符串替换函数
  • 【随机文章】
  • C#代表元及事件触发
  • 方正熊猫参数 ( 一)
  • 鸟哥下的DNS服务器的配制
  • 《再接再厉 又一套经典.net控件集:之二 for ASP NET》(Infragistics NetAdvantage 2005 Vol 1 ASP NET Fina...
  • 使用准则进行条件查询--1.4.从窗体中选择查询的条件
  • cpu核心类型
  • HTTP请求模型
  • ASP.NET 程序设计-序
  • 如何在PB中创建图标栏应用
  • Linux Command Line Tips
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.