shell基础练习1

执行shell

1.Shell执行三种方法

Chmod +x file 先修改权限

然后

  1. Sh file
  2. .file
  3. Source file

2.四种shell

系统需要哪种shell执行shell程序

#!/bin/bash

#!/bin/csh

#!/bin/pdksh

#!/bin/tcsh

3.Shell功能

  1. 命令解释执行
  2. 文件名替换
  3. 输入输出重定向
  4. 连通管道
  5. 系统环境设置
  6. Shell编程

4. Shell变量

1 . Vi/vim新建一个.sh文件

#!/bin/bash

echo  “hello”

"#!" 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种Shell。

echo命令用于向窗口输出文本

4.1 shell变量定义

定义变量时,变量名不加美元符号($,PHP语言中变量需要)

可以显示或语句定义变量

For file in’ ls  /etc’

4.2 使用变量

使用定义过的变量  前面加美元符号$

定义变量不用加$

#!/bin/bash

your_name="qinjx"

echo $your_name

echo ${ your_name}

变量名外面的花括号是可选的,加不加都行,加花括号是为了帮助解释器识别变量的边界,比如下面这种情况:(Ada、Coffe 、Action 、java是四个)

for skill in Ada Coffe Action Java; do

    echo "I am good at ${skill}Script"

done

输入:

#!/bin/bash

your_name="qinjx"

echo $your_name

echo ${your_name}

for skill in Ada Coffe Action Java; do

    echo "I am good at ${skill}Script"

done

输出结果:

qinjx
qinjx
I am good at AdaScript
I am good at CoffeScript
I am good at ActionScript
I am good at JavaScript

4.3 只读变量readonly(只能读,不能改变值)

Readonly

使用 readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变。不加$

输入:

#!/bin/bash

myUrl="http://www.w3cschool.cc"

readonly myUrl

myUrl=http://www.runoob.com

输出:

/usercode/file.sh: line 4: myUrl: readonly variable

4.4删除变量unset

删除变量 unset var_name  不加$

输入:

#!/bin/sh
myUrl="http://www.runoob.com"
unset myUrl
echo $myUrl

没有输出

4.5 变量类型

1) 局部变量 局部变量在脚本或命令中定义,仅在当前shell实例中有效,其他shell启动的程序不能访问局部变量。

2) 环境变量 所有的程序,包括shell启动的程序,都能访问环境变量,有些程序需要环境变量来保证其正常运行。必要的时候shell脚本也可以定义环境变量。

3) shell变量 shell变量是由shell程序设置的特殊变量。shell变量中有一部分是环境变量,有一部分是局部变量,这些变量保证了shell的正常运行

4.6 字符串

字符串可以用单引号,也可以用双引号,也可以不用引号。单双引号的区别跟PHP类似。

4.6.1单引号

  1. 1.       单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
  2. 单引号字串中不能出现单引号(对单引号使用转义符后也不行)。

Str=’abcde

输入:

#!/bin/sh

str='this is a string'

echo $str’

输出:

this is a string

输入:

#!/bin/sh

test='test1'

str='this is a string $test'

str2='this is a string test'

echo $str

echo $str2

输出:

this is a string $test
this is a string test

4.6.2双引号

双引号的优点:

1. 双引号里可以有变量 $your_name

2. 双引号里可以出现转义字符

输入:

#!/bin/sh

your_name='qinjx'

str="Hello, I know your are \"$your_name\"! \n"

str2="hello,i know your are $your_name"

str3="hello,i know your are\"$your_name\""

echo $str

echo $str2

echo $str3

输出:

Hello, I know your are "qinjx"! \n
hello,i know your are qinjx
hello,i know your are"qinjx"

4.6.3拼接字符串

定义变量时由另外一个变量和其他字符串拼接而成

Str =”test”

Str2=”this is the first str $Str2”

Str2 就为 this is the first test

输入:

#!/bin/sh

your_name="qinjx"

greeting="hello, "$your_name" !"

greeting_1="hello, ${your_name} !"

echo $greeting

echo $greeting_1

echo $greeting $greeting_1

输出:

hello, qinjx !
hello, qinjx !
hello, qinjx ! hello, qinjx !

可以看出:echo 输出可以拼接  定义变量可以拼接

4.6.4获取字符串长度

string=’abcd’  ——字符串长度4

方法一:echo  ${#字符串变量名} 如echo  ${#string}

string="abcd"

echo ${#string} #输出 4

方法二:echo  –n  $字符串变量名|wc –c 如echo  –n  $string |wc –c

方法三:echo  $string |awk  ‘{ print length ($0)}’

        ——求的是string这个变量的字符串长度

方法四:直接去字符串长度

echo abcd|awk  ‘{print length($0)}’——取的是abcd这个长度为4

输入:

#!/bin/sh

your_name="qinjx"

greeting="hello,"$your_name"!"

greeting_1="hello,${your_name} !"

echo $greeting

echo $greeting_1

echo $greeting $greeting_1

echo -n $greeting |wc –c  ——hello,ginjx! 12

echo ${#greeting}   ——hello,qinjx! 12

echo greeting|awk '{print length($0)}'

echo $greeting|awk '{print length($0)}'

输出:

hello,qinjx!
hello,qinjx !
hello,qinjx! hello,qinjx !
12
12
8
12

posted @ 2016-06-17 14:49  Mandy—AZA  阅读(215)  评论(0编辑  收藏  举报