Shell学习笔记(一)
来源:互联网 发布:sai绘图软件安卓版 编辑:程序博客网 时间:2024/05/09 07:13
#在改变桌面分辨率的时候遇到问题 桌面背景模糊 改变色彩深度解决问题
#echo换行 需要加上参数-e
#变量被引用需加上符号#
下面写一个简单的Shell脚本,在/home目录下创建一个Shelltest的文件夹
#!/bin/sh
#This is mkdir
cd /home
mkdir Shelltest
echo "Shelltest is created."
要注意Shell中的特殊符号
1、注释符#
除了#!/bin/bash里的#特殊
2、美元符 $
变量符。与反斜杠转义符相反,使其后的普通字符作为变量名,如$a表示变量a的值。变量字符长度超过1个时,用{}括起来
3、单引号
被引起的字符全部做普通字符,即全部原样
echo ‘my $SHELL’
4、双引号
引号内的内容,除$、转义符\、倒引号`这三个保留特殊功能,其他字符均做普通字符。
5、倒引号(数字1键旁边的那个键)
引号内的字符串当做shell命令行解释执行,得到的结果取代整个倒引号括起来的部分。
写一个例子
#!/bin/sh
echo 'my home is $home'
echo "my home is $home"
echo `my home is $home`
echo "my home is `pwd`"
echo 'my home is `pwd`'
输出结果:
因为第三行echo `my home is $home`
不可以执行所以会报错
写一个shell,判断文件是否存在 并检测语法是否真确:
#!/bin/sh
if [ $# -ne 1 ]
then echo "Usage -$0 filename"
elif [ -f $1 ]
then echo "$1 is exist!"
else echo "$1 is not exist!"
fi
下面写一个程序熟悉内部变量使用:
#!/bin/sh
echo "当前程序的名字是$0"
echo "共输入$#个参数,全部参数是$@,第一个参数是$1"
echo "当前进程的pid是$$"
echo "执行完毕,返回值是$?"
写一个计算器 输入两个数和一个操作符能进行加减乘除四则运算
#!/bin/sh
echo "please enter the first number:"
read a
echo "please enter the second number:"
read b
echo "please enter the opertion"
read opt
result1=$[ $a ${opt} $b ]
result2=$(( $a ${opt} $b ))
result3=`expr $a "\${opt}" $b` 当一个字符有特殊含义,需要在前面添加反斜线,防止shell误读,屏蔽特殊含义
如& * + … ^ $ ` " | ?
echo "the result1 of $a $opt $b = ${result1}"
echo "the result2 of $a $opt $b = ${result2}"
echo "the result3 of $a $opt $b = ${result3}"
其中result1,2,3分别采用不同的方法运算:
$[ a + b ]
$(( a + b ))
`expr a + b` 注意空格!
运行结果:
- shell学习笔记(一)
- Shell学习笔记(一)
- shell 学习笔记(一)
- shell 学习 笔记(一)
- shell学习笔记(一)
- Shell学习笔记(一)
- shell学习笔记(一)
- shell学习笔记(一)
- shell学习笔记(一)
- shell学习笔记《一》
- Shell学习笔记一
- shell学习笔记一
- Linux Shell学习笔记 (一)
- UNIX Shell编程 学习笔记(一)
- learning bash shell 学习笔记(一)
- shell脚本学习笔记 (一)
- Linux Shell学习笔记(一)
- Linux学习笔记(一)----Shell
- (转载保存)iOS开发中不要使用相对路径
- java Lambda入门
- sqoop 相关系列,基本操作
- C# 如何创建与读写txt文件
- Attempted read from closed stream.问题
- Shell学习笔记(一)
- 获取虚拟按键、状态栏等高度
- HDU 3709 Balanced Number(枚举思想+数位DP)
- Ambari学习11_安装过程[Errno 256] No more mirrors to try问题解决
- "npm ERR! Error: EPERM: operation not permitted"问题解决
- windows下配置mysql zip格式
- MVC模式解读
- Windows下配置nutch
- Struts2拦截器