echo:显示文本行或变量取值,或者把字符串输入到文件中

echo中常用功能:\t 跳格  \n 换行  必须使用 -e才能转义符生效

echo $变量名 变量前必须加上$age=18   echo $age
echo -e 输出变量,并跳格 \techo -e  “hello\tworld”
输出变量,并换行 \n

read:从键盘或者文件的某一行文本中读入信息,并将其赋给一个变量;

输入内容以空格分隔,与变量一一对应。

如果输入的值个数多于变量个数,多余的值会赋给最后一个变量。

如果输入的值个数少于变量个数,按空格一一对应赋值,多余的变量为空



read var

赋值 read name

tom

echo $name

read var1 var2 "输入的值多余变量个数, read name age

多余的值赋值给最后一个变量" hyh 12 ttt    //赋值

echo $age  
12 ttt          //输出j结果  

tee:把一个命令的输出内容拷贝到另一个文件,一般和管道符’|’一起使用  

-a 表示将内容追加到文件末尾


file 表示保存输出结果的文件(文件所在路径必须存在,文件存在与否不作硬性要求)


tee tee -a file 把一个命令的输出内容拷贝到另一个文件 echo "helloworld" | tee -a home1


command > file

输出内容写入到一个文件,错误仍然输出屏幕 cat hello.txt > hello1.txt


command >>file

输出内容追加写入到一个文件 cat hello.txt >> hello1.txt


command 1> file

输出内容向到一个文件 cat hello.txt 1> hello1.txt


command 2>> file

错误内容追加写入到一个文件 cat hello 2>> hello1.txt


command >file 2>&1

输出内容和错误内容一起写入到一个文件 cat hello hello.txt > hello1.txt 2>&1


command >>file 2>&1

输出内容和错误内容一起追加写入到一个文件 cat hello hello.txt >> hello1.txt 2>&1


command 1> file1 2> file2

输出内容写入一个文件,错误内容写入另一个文件 cat hello hello.txt 1> hello1.txt 2> hello2.txt


command < file

以file 文件内容做为文件输入 read name age < user.txt


comamnd < file1 > file2

以file1 文件内容做为文件输入,file2做为输出 cat <user.txt > user1.txt


Shell后台命令


crontab执行步骤:

1.进入管理员权限,执行命令 yum -y install vixie-cron 安装crontab

2.输入命令 service crond status 查看crontab服务是否启动;

    可通过命令 service crond start\stop\restart 对服务进行启动、关闭和重启

注:通过Yum安装的服务在service命令中,服务名后都要加d,如crond

3.在crontab后台执行命令

4.创建Shell脚本,一般以file.sh命名并编写操作命令。(脚本中的路径皆为绝对路径)

5.通过命令直接执行脚本或将执行命令放入到后台执行程序中执行

    方式一: 路径+脚本名(要求脚本必须有执行权限) => ./helloworld.sh

   方式二:sh + 脚本名  =>  sh helloworld.sh 

crontab 格式: 分 时 日 月 星期 运行命令

1、表示范围:周一到周五 => 1-5


2、表示某些值: 10点和 20点 => 10,20

3、表示每隔某些值: 每隔5分钟 => */5


7月 和 10月 的 4-10 日这期间的 23点 和 3点 ,每隔 15分钟执行一次输出helloworld


crontab -e

打开crontab脚本 执行shell脚本 */15 3,23 4-10 7,10 * /home/hyh/class19/shell/backup.sh


at

后台执行命令,只执行一次


"$at 19:10 2017-7-5 echo "hello" ctrl+ d

//结束编辑


atq / at -l

查看任务


at -c

任务编号 查看某个具体任务


atrm

删除某个任务


at 19:10 2017-7-5 -f

执行脚本 "at 10:15 2017-7-5 -f  /home/hyh/class19/shell/edit.sh"


系统-用户变量



export 变量名 
  age=18 export age 
 将变量age变为系统变量 

unset 变量名 
  释放变量 
 unset age 

env 
 查看系统变量 
env | grep age 

set 
 查看用户变量 
set | grep age 

readonly 变量名 
  readonly age



位置-特定变量 

位置变量:只读变量,向shell脚本传递参数,参数个数可以任意多,但只有前9个有效表示形式为: $1,$2.....$9


特定变量:只读变量,反映脚本运行过程中的控制信息

$! :显示后台最后运行的一个进程的进程号

$# :显示传递到shell脚本中参数的总个数

$@:显示所有的参数,使用时加引号,并返回每个参数

$* :显示所有的参数

$$ :脚本运行的当前进程号

$? :显示最后执行的命令的状态,0为正确,非0为错误



#!/bin/bash 
  shell/local.sh 2 2 3 4 5 6 7 8 9 10 11 
  

echo "give 1 you have $1" 
give 1 you have 2 
  

echo "give 2 you have $2" 
give 2 you have 2 
  

echo "give 3 you have $3" 
give 3 you have 3 
  

echo "give 4 you have $4" 
give 4 you have 4 
  

echo "give 5 you have $5" 
give 5 you have 5 
  

echo "give 6 you have $6" 
give 6 you have 6 
  

echo "give 7 you have $7" 
give 7 you have 7 
  

echo "give 8 you have $8" 
give 8 you have 8 
  

echo "give 9 you have $9" 
give 9 you have 9 
  

echo "give 10 you have $10" 
give 10 you have 20 
  

echo "give 11 you have $11" 
give 11 you have 21 
  

echo "give \$# you have $#" 
give $# you have 11 
  

echo "give \$@ you have $@" 
give $@ you have 2 2 3 4 5 6 7 8 9 10 11 
  

echo "give \$* you have $*" 
give $* you have 2 2 3 4 5 6 7 8 9 10 11 
  

echo "give \$! you have $!" 
give $! you have 
  

echo "give \$$ you have $$" 
give $$ you have 4709 
  

echo "give \$? you have $?" 
give $? you have 0