Linux 脚本编写基础
来源:互联网 发布:维达网络计划图软件 编辑:程序博客网 时间:2024/06/05 18:08
1.1 语法基本介绍
1.1.1 开头
程序必须以下面的行开始(必须方在文件的第一行):
#!/bin/sh
符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中我们使用/bin/sh来执行程序。(可以运行echo $SHELL来看你的系统是使用那一类shell, 比如ubuntu使用的是/bin/bash,则第一行写成#!/bin/bash)
当编辑好脚本时,如果要执行该脚本,还必须使其可执行。 要使脚本可执行:
编译 chmod +x filename 这样才能用./filename 来运行
1.1.2 注释
在进行shell编程时,以#开头的句子表示注释,直到这一行的结束。我们真诚地建议您在程序中使用注释。
如果您使用了注释,那么即使相当长的时间内没有使用该脚本,您也能在很短的时间内明白该脚本的作用及工作原理。
1.1.3 变量
在其他编程语言中您必须使用变量。在shell编程中,所有的变量都由字符串组成,并且您不需要对变量
进行声明。要赋值给一个变量,您可以这样写:
#!/bin/sh
#对变量赋值(注意等号前后不能有空格,因为加空格后a被认为是命令名而不是一个变量):
a="hello world"
# 现在打印变量a的内容:
echo "A is:"
echo $a
echo $a
有时候变量名很容易与其他文字混淆,比如:
num=2
echo "this is the $numnd"
echo "this is the $numnd"
这并不会打印出”this is the 2nd”,而仅仅打印”this is the “,因为shell会去搜索变量numnd的值,
但是这个变量时没有值的。可以使用花括号来告诉shell我们要打印的是num变量:
num=2
echo "this is the ${num}nd"
echo "this is the ${num}nd"
这将打印: this is the 2nd
赋值
let num=num+1
echo $num
echo $num
打印: 3
更多请访问bcoder编程网原文:
http://www.bcoder.cn/linuxshell/
0 0
- Linux 脚本编写基础
- Linux 脚本编写基础
- Linux脚本编写基础
- Linux 脚本编写基础
- linux:脚本编写基础
- Linux脚本编写基础
- Linux 脚本编写基础
- Linux 脚本编写基础
- linux脚本编写基础
- Linux 脚本编写基础
- Linux 脚本编写基础
- Linux脚本编写基础
- Linux 脚本编写基础
- Linux 脚本编写基础
- Linux 脚本编写基础
- Linux 脚本编写基础
- Linux 脚本编写基础
- Linux 脚本编写基础
- WINDOW 安装pear
- php中正则表达式基本语法
- socket中的函数遇见EINTR的处理
- Single Number II
- High-dimensional model representation (HDMR)
- Linux 脚本编写基础
- linux上检测代码性能以及内存泄露
- Android继承Parcelable接口传递复杂数据类型
- Android中的SharedPreferences存储数据方式
- Android 集成Valgrind内存泄露检测工具
- XML操作
- sbt环境搭建
- 更改xshell转发规则
- Java Mocking入门—使用Mockito