just a note

来源:互联网 发布:网络上传速度 编辑:程序博客网 时间:2024/06/09 18:25
1.Shell: 命令行界面下 让我们与系统沟通的一个工具接口(换句话说,就是我们平时操作系统,做能通过安装软件,鼠标双击等动作来让系统完成一些事情。
         而Shell则是一个让我们能更多地操作系统 让它执行更加复杂一点的,非应用级别的任务)
     baidu:(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器)。它类似于DOS下的command和后来的cmd.exe。
     它接收用户命令,然后调用相应的应用程序。
     shell提供了你与操作系统之间通讯的方式。这种通讯可以以交互方式(从键盘输入,并且可以立即得到响应),
     或者以shell script(非交互)方式执行。shell script是放在文件中的一串shell和操作系统命令,它们可以被重复使用。
     本质上,shell script是命令行命令简单的组合到一个文件里面。

     交互式shell和非交互式shell
     交互式模式就是shell等待你的输入,并且执行你提交的命令。这种模式被称作交互式是因为shell与用户进行交互。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、签退。当你签退后,shell也终止了。
     shell也可以运行在另外一种模式:非交互式模式。在这种模式下,shell不与你进行交互,而是读取存放在文件中的命令,并且执行它们。当它读到文件的结尾,shell也就终止了。


 
  Script; 则是让shell去执行任务的语言,主要是shell的命令,搭配正则表达式和管道命令(这些让语言提供了逻辑)
 

2.不适合处理大量数据,因为会使用较多CPU资源。
----------------------------------------------------------------

1.PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  export PATH

2.read -p
  echo -e
3.变量语法:
    3.1 变量赋值:=sth or ="sth"or='sth'双引号 特殊字符仍是转义,单引号内则是文本,单双引号内能有空格
    3.2 $()是在变量中嵌套命令。 如 verion=$(cat file1)或者 version=$`cat file1`
    3.2 在变量中引用其他已定义的变量:$变量名 或者 ${变量名} 如:PATH="$PATH":/home/bin 或者PATH={$PATH:/home/bin}
    3.3 unset 取消变量, 自定义变量名字小写; 在其他子程序用变量得export
4.用正则表达式验证:
  date2=201405qq
  date_d=$(echo $date2 |grep '[0-9]\{8\}')
  echo $date_d
5.声明
  declare/typeset  -i 整数;-a 数组;-x export;http://www.cnblogs.com/fhefh/archive/2011/04/22/2024857.html
6.不断行输出和将小写转换成大写
  echo -n "Your chioce is: "
  echo $1 | tr 'a-z' 'A-Z'

 


----------------------------------------------------------------------------------
三种执行方式:
1.    bash sh04.sh    会在子bash进程中执行,结束后设置的环境变量不会生效(在另一个bash中查不到)
2.    ./sh04.sh    会在子bash进程中执行,结束后设置的环境变量不会生效
3.    source sh04.sh  会在父bash进程中执行,结束后设置的环境变量仍然生效
----------------------------------------------------------------------------------
判断存不存在:
1.判断变量存不存在
  filename=${filenameInput:-"filename"}
2.判断文件存不存在/文件的的权限/文件之间的比较
  test [-e -f -d] /filename( $?; &&; || 这三个管道,数据流重定向命令如何使用?)
  test -r file 文件有读的权限
3.判断两个整数,字符串之间的比较和判定
  test n1 -eq n2; test -z string; test str1 = str2;
4.多重条件判断
  test -r file [-a,-o,!] -x file 文件有读且有执行的权限
5.[] 判断存不存在
 
6.条件判断式 一
  a. if ***; then ***; fi;
  b. if ***; then ***; elif ***; then ***; else; fi;
7.条件判断式 二
  case $var in
      "case1")
        doing something
          ;;
      "case2")
        doing something
          ;;
      *) #not meet any case above
        doing someting
          ;;
  esac
8.function功能
  function fname(){
    doing something
  }
9.循环
  a.until [ "$i" == $1 ]
    do
            i=$(($i+1))
            s=$(($s+$i))
    done

  b.while [ "$i" == $1 ]
    do
            i=$(($i+1))
            s=$(($s+$i))
    done
  c.已知循环次数(in 后面有几个参数就几次),以此 var =con n,将var带入action中计算
    for var in con1 con2 con3...
    do
    action
    done
  d.已知循环次数(in 后面有几个参数就几次),以此 var =con n,将var带入action中计算
    for var in con1 con2 con3...
    do
    action
    done
    
    已知循环次数2
    for ((i=1; i<=$nu; i=i+1))
    do
            s=$(($s+$i))
    done
    echo "The result of '1+2+3+...$nu' is : $s"


10.


   

 
----------------------------------------------------------------------------------
demo1:
#!/bin/bash
read -p "Please input your first name:" firstname
read -p "Please input your last name: " lastname
echo -e "\nYour full name is: $firstname $lastname"
-------
demo2:
#!/bin/bash
echo -e "I will use 'touch' to create 3 files"
read -p "Please input your filename:" filenameInput

filename=${filenameInput:-"filename"} 检查filename这个变量存不存, 不存在则set值

date1=$(date --date='2 days ago' +%Y%m%d)括号内是个date的命令
date2=$(date --date='1 day ago' +%Y%m%d)
date3=$(date +%Y%m%d)
file1=${filename}${date1}
file2=${filename}${date2}
file3=${filename}${date3}

touch "$file1"
touch "$file2"
touch "$file3"
------
demo3:
$ vi sh04.sh
#!/bin/bash
#!/bin/bash
echo - e "Please input two numbers \n"
read -p "first number: " first
read -p "second number: " second
declare -i total2=$first*$second
total3=$first*$second
echo -e "\n The result is $total"
echo -e "\n The result is $total2"
echo -e "\n The result is $total3"
echo $((4*5))
------
demo4
$ vi sh05.sh
#!/bin/bash
echo "This is a script to explore a filename"
read -p "Please input a fileName " filename
test -z $filename && echo "Please input a fileName" && exit 0
test ! -e $filename && "This filename does not exsit" && exit 0
test -f $filename && fileType=regularFile
test -d $filename && fileType=floder
test -r $filename && filePerm=read
test -w $filename && filePerm=$filePerm" write"
test -x $filename && filePerm=$filePerm" excute"
echo "The properties of this file is: $filname $fileType $filePerm"
-----
demo5
$ vi sh06.sh
#!/bin/bash
read -p "Do you really want to do this? 'Y' for yes,'N' for no " answer
[ $answer=="Y" -o $answer=="y" ] && echo $? &&  echo "OK, please continue" && exit 0
[ $answer=="N" -o $answer=="n" ] && echo $? && echo "Oh, quit now" && exit 0
echo "input cannot handle" $$ exit 0

-----
demo6
$ vi sh07.sh
#!/bin/bash
echo "The script name is ==> $0"
echo "The number of parameters is --> $#"
echo "The first parameter is --> $1"
[ "$#" -lt 2 ] && echo "The number of parameters is less than 2." && exit 0
echo "Your whole parameters are --> $@"
shift
echo  "shift"
echo "The number of parameters is --> $#"
echo "Your whole parameters are --> $@"
shift 3 #
echo "shift"
echo "The number of parameters is --> $#"
echo "Your whole parameters are --> $@"
-----
demo7
$ vi sh08.sh
#!/bin/bash
if [ "$1" == "hello" ]; then
echo "Hello, how are you ?"
elif [ $# -lt 1 ]; then
echo "You must input a paremater"
else
echo "The only parameter is 'hello', ex> {$0 hello}"
fi
-----
demo8
$ vi sh09.sh
#!/bin/bash
echo  "This script will print your selections !"
# read -p "input your chioce please: " chioce
# case chioce in
case $1 in
"one")
        echo "Your chioce is $1"
        ;;
"two")
        echo "Your chioce is $1"
        ;;
"three")
        echo "Your chioce is $1"
        ;;
*)
        echo "Usage $0 {one|two|three}"
        ;;
esac
-----
demo10
$ vi sh10.sh
#!/bin/bash
function print(){
        echo -n "Your chioce is: "
        echo $1 | tr 'a-z' 'A-Z'
}

echo "This scriptis going to print your choice: "
case $1 in
"one")
        print one
        ;;
"two")
        print two
        ;;
"three")
        print thress
        ;;
*)
        echo "Usage: $0 {one|two|three}"
        ;;
esac
-----
demo11
$ vi sh11.sh
!#/bin/bash
s=0
i=0
while [ "$i" != $1 ]
do
        i=$(($i+1))
        s=$(($s+$i))
done
echo -n "The result of 1+2+...+"
echo $1
echo " is "
echo $s
-----
demo12
$ vi sh12.sh
!#/bin/bash
#!/bin/bash
#!/bin/bash
s=0
i=0
until [ "$i" == $1 ]
do
        i=$(($i+1))
        s=$(($s+$i))
done
echo -n "The result of 1+2+...+"
echo $1
echo " is "
echo $s
------
demo13
#!/bin/bash
while [ "$answer" != "yes" ]
do
        read -p "please input your selection: " answer
done
echo "OK, you are done!"
-----
demo14
#!/bin/bash
for animal in dog cat elephant
do
        echo "there are ${animal}s..."
done
----
#!/bin/bash
read -p "please input a number: " nu
s=0
for ((i=1; i<=$nu; i=i+1))
do
        s=$(($s+$i))
done
echo "The result of '1+2+3+...$nu' is : $s"


0 0
原创粉丝点击