当前位置: 首页 > news >正文

shell script 入门 笔记

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 ']'




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

)遇见的两个小孩。。

。多么美好年纪啊。。。大笑


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

转载于:https://www.cnblogs.com/blfshiye/p/4629530.html

相关文章:

  • Dynamics AX 2012 R2 客制化RDP报表参数对话框
  • BZOJ 1052 HAOI2007 覆盖问题 二分法答案+DFS
  • Alwasyon环境下增加数据文件需要注意的几点
  • 学习笔记_过滤器概述(过滤器JavaWeb三大组件之一)
  • ldd查询命令或软件共享的函数库(动态)
  • 员工考勤系统
  • C# 打印本地PDF文件
  • javascript的位操作、整数、二进制
  • .net 按比例显示图片的缩略图
  • 线上解决问题分析
  • JavaScript定时机制setTimeout与setInterval研究
  • UVA 11174 Stand in a Line 树dp+算
  • HttpSessionListener的用法
  • JasperReports报表组15
  • BZOJ 1264: [AHOI2006]基因匹配Match( LCS )
  • 分享一款快速APP功能测试工具
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • create-react-app做的留言板
  • Effective Java 笔记(一)
  • go语言学习初探(一)
  • mac修复ab及siege安装
  • Redis中的lru算法实现
  • SpiderData 2019年2月25日 DApp数据排行榜
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 翻译--Thinking in React
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 前端技术周刊 2019-02-11 Serverless
  • 巧用 TypeScript (一)
  • 数据结构java版之冒泡排序及优化
  • 我有几个粽子,和一个故事
  • 再次简单明了总结flex布局,一看就懂...
  • ​批处理文件中的errorlevel用法
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • (02)Hive SQL编译成MapReduce任务的过程
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (SpringBoot)第七章:SpringBoot日志文件
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (附源码)php新闻发布平台 毕业设计 141646
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (接口自动化)Python3操作MySQL数据库
  • (十)DDRC架构组成、效率Efficiency及功能实现
  • (一)Thymeleaf用法——Thymeleaf简介
  • (转)Windows2003安全设置/维护
  • .NET Core日志内容详解,详解不同日志级别的区别和有关日志记录的实用工具和第三方库详解与示例
  • .NET Windows:删除文件夹后立即判断,有可能依然存在
  • .NET 回调、接口回调、 委托
  • .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
  • .NetCore Flurl.Http 升级到4.0后 https 无法建立SSL连接
  • @requestBody写与不写的情况
  • [100天算法】-目标和(day 79)
  • [AUTOSAR][诊断管理][ECU][$37] 请求退出传输。终止数据传输的(上传/下载)
  • [bzoj 3534][Sdoi2014] 重建
  • [bzoj4240] 有趣的家庭菜园
  • [C#]C# OpenVINO部署yolov8图像分类模型