shell script 入门

在 shell script 注意必须使用完全相同写在下面:

1.  指令的运行是从上而下、从左而右的分析与运行;

2.  指令的运行就如同第五章内提到的: 指令、选项不參数间的多个空白都会被忽略掉;

3.  空白行也将被忽略掉,并且 [tab] 按键所推开的空白相同规为空格键;

4.  假设读取到一个 Enter 符号 (CR) 。就尝试開始运行该行 (或该串) 命令;


5.  至亍假设一行的内容太多,则能够使用『 \[Enter] 』来延伸至下一行。

6.  『 # 』可做为批注。不论什么加在 # 后面的资料将所有被规为批注文字而被忽略。

第一个shell script。必须是hello world。哈哈

#!/bin/bash

PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/bin

export PATH
echo -e "hello world! \n"
exit 0

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh01.sh

hello world!

特此说明一下,Ubuntu默认的shell是dash,而不是bash。echo -e时候会有问题。会把-e也打印出来。



转载link:http://webnoties.blog.163.com/blog/static/1835251412013518362635/

非常谢谢作者的blog帮我搞定了这个默认shell不是bash的问题

由于ubuntu默认的sh是连接到dash的,又由于dash跟bash的不兼容所以出错了.运行时能够把sh换成bash 文件名称.sh来运行.成功.dash是什么东西,查了一下,应该也是一种shell,貌似用户对它的诟病颇多.

by the way改动sh默认连接到bash的一种方法:

sudo dpkg-reconfigure dash

选择no就可以.

#! /bin/bash

# code writer : EOF
# code date : 2014.07.29
# code file : sh02.sh
# e-mail : jasonleaster@gmail.com
# code purpose:
# This program would ask the user to input some varible's value
# and then print them out into the screen. PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/bin
export PATH read -p "Please input your first name:" first_name #give the user a message whatshould be inputed
read -p "Please input your second name" second_name
echo -e "\nYour full name is $first_name $second_name" #print out the user's name

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh02.sh

Please input your first name:Jason

Please input your second nameLeaster



Your full name is Jason Leaster

#! /bin/bash

# code writer : EOF
# code date : 2014.07.29
# code file : sh04.sh
# e-mail : jasonleaster@gmail.com
# code purpose:
# This program was coded for demonstrating some
# mathmatical operations on varibles which in bash script
# If you find something wrong with my code, please touch me.
# Thank you! PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/bin
export PATH echo -e "You SHOULD input 2 numbers ,I will cross them !\n"
read -p "first number: " first_num
read -p "second number: " second_num total=$(($first_num*second_num)) echo -e "total is : $total"

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh04.sh

You SHOULD input 2 numbers ,I will cross them !



first number: 10

second number: 25

total is : 250





不怕丢人的说,上面这地方写成$(total)差点纠结而shi。。

#! /bin/bash

# code writer : EOF
# code date : 2014.07.29
# code file : sh05.sh
# e-mail : jasonleaster@gmail.com
# code purpose:
# This program was coded for a demo for command -- test
# If you find something wrong with my code, please touch me.
# Thank you! PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/bin
export PATH echo -e "Please input a filename, I will check the filename's type and permission.\n\n"
read -p "Input a filename : " filename test -z $filename && echo "You must input a filename." && exit 0 test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0 test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="writable"
test -x $filename && perm="excutable" echo "The filename: $filename is a $filetype"
echo "And the permissions are: $perm"

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh05.sh

Please input a filename, I will check the filename's type and permission.





Input a filename : sh01.sh

The filename: sh01.sh is a regulare file

And the permissions are: writable

删除一个环境变量用      unset 环境变量名

#! /bin/bash

# code writer : EOF
# code date : 2014.07.29
# code file : sh06.sh
# e-mail : jasonleaster@gmail.com
# code purpose:
# This program was coded for a demo for [ A==B ]
# If you find something wrong with my code, please touch me.
# Thank you! PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/bin
export PATH read -p "Please input (Y/N) : " yn
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0
[ "$yn" == "N" -o "$yn" == "n" ] && echo "NO,interrupt" && exit 0
echo "I don't know what you choice is" && exit 0

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh06.sh

Please input (Y/N) : y

OK, continue

#! /bin/bash

# code writer : EOF
# code date : 2014.07.29
# code file : sh07.sh
# e-mail : jasonleaster@gmail.com
# code purpose:
# This program was coded for a demo that user input a file name
# and some parameters, lately program process it and print out the file
# type and permission.
# If you find something wrong with my code, please touch me.
# Thank you! PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/bin
export PATH echo "The script name is ==> $0"
echo "Total parameter numbe is ==> $#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here" && exit 0
echo "Your whole parameter is ==> $@"
echo "1st parameter ==> $1"
echo "2nd parameter ==> $2"

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh07.sh

The script name is ==> ./sh07.sh

Total parameter numbe is ==> 0

The number of parameter is less than 2. Stop here

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh07.sh hello world

The script name is ==> ./sh07.sh

Total parameter numbe is ==> 2

Your whole parameter is ==> hello world

1st parameter ==> hello

2nd parameter ==> world

#! /bin/bash

# code writer : EOF
# code date : 2014.07.30
# code file : sh08.sh
# e-mail : jasonleaster@gmail.com
# code purpose:
# This program was coded for a demo for command -- shift
# If you find something wrong with my code, please touch me.
# Thank you! PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/bin
export PATH echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> $@"
shift
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> $@"
shift 3
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> $@"

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh08.sh

Total parameter number is ==> 0

Your whole parameter is ==>

Total parameter number is ==> 0

Your whole parameter is ==>

Total parameter number is ==> 0

Your whole parameter is ==>

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh08.sh hello world

Total parameter number is ==> 2

Your whole parameter is ==> hello world

Total parameter number is ==> 1

Your whole parameter is ==> world

Total parameter number is ==> 1

Your whole parameter is ==> world

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh08.sh hello world jason leaster tonight

Total parameter number is ==> 5

Your whole parameter is ==> hello world jason leaster tonight

Total parameter number is ==> 4

Your whole parameter is ==> world jason leaster tonight

Total parameter number is ==> 1

Your whole parameter is ==> tonight

有意思的是假设文件名称之后没有“參数”,parameter number就是0,假设參数少于shift移动的距离(例如说代码中的shift 3),就会保留最低数目为1,假设參数少于shift移动的距离

#! /bin/bash

# code writer : EOF
# code date : 2014.07.30
# code file : sh08.sh
# e-mail : jasonleaster@gmail.com
# code purpose:
# This program was coded for a demo for "if[] ; then fi"
# If you find something wrong with my code, please touch me.
# Thank you! PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/bin
export PATH read -p "Please input (Y/N) :" yn if [ "$yn" == "Y" -o "$yn" == "y" ]; then
echo "OK,continue"
exit 0
fi if [ "$yn" == "N" -o "$yn" == "n" ]; then
echo "Oh,interrupt!"
exit 0
fi echo "I don't know what your choice is" && exit 0

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh09.sh

Please input (Y/N) :y

OK,continue

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh09.sh

Please input (Y/N) :n

Oh,interrupt!

#! /bin/bash

# code writer : EOF
# code date : 2014.07.30
# code file : sh9.sh
# e-mail : jasonleaster@gmail.com
# code purpose:
# This program was coded for a demo for "if[] ; elif [] ;then else fi"
# If you find something wrong with my code, please touch me.
# Thank you! PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/bin
export PATH read -p "Please input (Y/N) :" yn if [ "$yn" == "Y" -o "$yn" == "y" ]; then
echo "OK,continue"
exit 0
elif [ "$yn" == "N" -o "$yn" == "n" ];then
echo "Oh,interrupt!"
exit 0
else
echo "Are you kidding me? You don't know what means \"Input (Y/N)\" \n"
fi echo "I don't know what your choice is" && exit 0

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh10.sh

Please input (Y/N) :hehe

Are you kidding me? You don't know what means "Input (Y/N)" \n

I don't know what your choice is

#! /bin/bash

# code writer : EOF
# code date : 2014.07.30
# code file : sh10.sh
# e-mail : jasonleaster@gmail.com
# code purpose:
# This program was coded for a demo for "if[] ; elif [] ;then else fi"
# If you find something wrong with my code, please touch me.
# Thank you! PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/bin
export PATH echo "This program will print your selection !" case $1 in
"one")
echo "Your choice is ONE"
;;
"two")
echo "Your choice is TWO"
;;
*)
echo "42"
;;
esac

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh12.sh hello world

This program will print your selection !

42

#! /bin/bash

# code writer : EOF
# code date : 2014.07.30
# code file : sh13.sh
# e-mail : jasonleaster@gmail.com
# code purpose:
# This program was coded for a demo for "function and while loop"
# If you find something wrong with my code, please touch me.
# Thank you! PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/bin
export PATH function secret()
{
echo "hello world!"
} temp=10 while [ $temp != 0 ]
do
secret
temp=$(($temp-1))
echo "$temp"
done

jasonleaster@ubuntu:~/shell_script_beginner$ sh ./sh13.sh

hello world!

9

hello world!

8

hello world!

7

hello world!

6

hello world!

5

hello world!

4

hello world!

3

hello world!

2

hello world!

1

hello world!

0

最后sh -x将使用do啊的script 内容显示到屏幕上,这是非常实用的參数,而-n不会显示不论什么信息

jasonleaster@ubuntu:~/shell_script_beginner$ sh -x ./sh13.sh

+ PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:/home/liuzjian/bin

+ export PATH

+ temp=2

+ '[' 2 '!=' 0 ']'

+ secret

+ echo 'hello world!'

hello world!

+ temp=1

+ echo 1

1

+ '[' 1 '!=' 0 ']'

+ secret

+ echo 'hello world!'

hello world!

+ temp=0

+ echo 0

0

+ '[' 0 '!=' 0 ']'

去衡山的时候,在白龙潭(应该是吧。不记得啦。。。

)遇见的两个小孩。。

。多么美好年纪啊。。。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

shell script 入门 笔记的更多相关文章

  1. linux基础之Shell Script入门介绍

    本文介绍下,学习shell script编程的入门知识,通过几个入门实例,带领大家走进shell script的神圣殿堂,呵呵,有需要的朋友参考下. 本文转自:http://www.jbxue.com ...

  2. shell script入门

    从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...

  3. bash shell学习-shell script基础 (笔记)

    A chain no stronger than its weakest link. "一着不慎,满盘皆输" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版)  Linux ...

  4. Shell Script 入门教程

    和 Shell 的区别 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁. Shell 即是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提 ...

  5. shell script简单笔记

    变量 shell script是一种脚本语言,变量的定义是通过 myName=HYB 这样的形式定义的. 当存在空格时,可以通过双引号或单引号将其变为字符串.双引号不进行转义,单引号将内容进行转义为一 ...

  6. shell script 学习笔记-----if,for,while,case语句

    1.if内的判断条件为逻辑运算: 2.if内的判断条件为目录是否存在,文件是否存在,下图先检验目录/home/monster是否存在,然后再检测/home/monster中的file.txt文件是否存 ...

  7. shell script 学习笔记-----shell变量

    1.在赋值语句name=value中不能存在空格,例如:name = value这样的形式会被认为是三个变量,因为本质上来说,脚本的内容就是传给shell程序的变量,而变量之间是通过空格区分的.如果想 ...

  8. shell script 学习笔记-----命令执行

    1.PATH变量:shell最主要的功能就是执行用户输入的命令,例如当用户输入一条“ls”命令之后,shell就要找到该命令对应的文件并执行.通常shell都会设置一个名叫PATH的环境变量,其中保存 ...

  9. shell script 学习笔记-----标准输出

    1.将标准输出(stdout)和标准错误输出(stderr)分别重定向到两个不同的文件 其中符号'>'默认将标准输出重定向,意思和'1>'相同,‘2>'表示重定向标准错误输出,数字1 ...

随机推荐

  1. 用cocos2d-x 3.2 实现的FlappyBird

    近期才開始学cocos2dx,买了几本书还有看大神(主要是 笨木头)的博客.然后就自己尝试用cocos2d-x实现了一下... (新手,勿喷...) 先看执行效果 http://pan.baidu.c ...

  2. PostgreSQL服务端监听设置及client连接方法

    背景介绍: PostgreSQL服务端执行在RedHat Linux上,IP为:192.168.230.128 client安装在Windows XP上, IP为:192.168.230.1 配置方法 ...

  3. spring的长处 ioc aop

    spring 的长处? 1.减少了组件之间的耦合性 ,实现了软件各层之间的解耦 2.能够使用easy提供的众多服务.如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它非 ...

  4. arcgis jsapi 调用google地区服务

    做地理信息系统(GIS)项目,除了实现功能用户体验度要好之外,最重要的是地图渲染效果更要好.很多时候苦于数据的完整性和对于配图的审美观,程序猿们都很难配出好看的地图效果.基于上述一般直接调用googl ...

  5. POJThe Doors AND NYIST 有趣的问题

    POJThe Doors AND NYIST 有趣的问题 题目链接:pid=227" target="_blank">Click Here~ 题目分析: 给你横纵坐 ...

  6. mysql经常使用的命令

    如何登陆数据库     飞机着陆     mysql -u <username> -p     访问本机数据库     mysql -u <username> -D <d ...

  7. 浅谈android的am命令

    android系统为大家提供了adb工具,在adb的基础上执行adb shell就可以从PC上对手机侧执行shell命令.和pc的linux系统一样,在系统的默认路径syste/bin下面是可执行程序 ...

  8. ThinkPHP的全部配置选项

    return array( /* Dispatch设置 */ 'DISPATCH_ON' => true, // 是否启用Dispatcher // URL模式: 0 普通模式 1 PATHIN ...

  9. 获取编译学习笔记 (六)—— si、di,双环

    疯狂暑期学习  汇编入门学习笔记 (六)-- si.di,双重循环 參考: <汇编语言> 王爽 第7章 1. and和or指令,与[bx+idata] and和or.就不多说了. [bx+ ...

  10. [PATCH] UBUNTU: SAUCE: (no-up) apparmor: Sync to apparmor3 - RC1(v3.4.x kernel)

    ubuntu touch v3.4 kernel AppArmor v3 backport patch 地址1:https://github.com/multirom-aries/ubuntu-pho ...