计算机组成
硬件-->内核[驱动]-->shell或图形-->人
shell
#cat /etc/shells
bash-completion[rhel7]
命令和路径
选项
/etc/profile 环境变量和启动程序
/etc/bashrc 函数和别名
~/.bash_profile
~/.bashrc
#vim /etc/profile
HISTSIZE=1000 命令历史的条目
标准输出(1),错误输出(2), 两者(&)
#ls install.log >log
#ls tabasfd 2>log
#ls install.log abc >log1 2>log2
#ls install.log abc &>logs
#ls install.log ttt >mylog 2>&1
#ls install.log 2>> logs
#ls install.log > /dev/null
#mail -s "test" root </etc/passwd
#vim test1.sh
#!/bin/bash
for i in {1..10}
do
useradd u$i 2>>user.log
echo "123" |passwd --stdin u$i >>pass.log12>>pass.log2
done
#chmod +x test1.sh
#vim test2.sh
#!/bin/bash
echo "hello the world"
脚本的执行:
一、需要权限(chmod +x test2.sh)
1.绝对路径
#/var/tmp/test2.sh
2.相对路径
#cd /var/tmp
#./test2.sh
#ln -s /var/tmp/test2.sh /bin/
#test2.sh
二、不需要脚本文件有x权限
1.会启动子进程 HISTSIZE=2
#bash test2.sh
#sh test2.sh
2.不会启动子进程
#. test2.sh
#source test2.sh
使用脚本配置yum源:
#!/bin/bash
rm -rf /etc/yum.repos.d/*
echo '
[dvd]
name=test
baseurl=ftp://192.168.4.254/rhel/
gpgcheck=0
' >/etc/yum.repos.d/dvd2.repo
++++++++++++++++++++++++++++++++
SHELL变量--shell的变量是弱类型的变量
定义变量
#变量名称=值
查看变量的值
#echo $变量的名称
取消变量
#unset 变量名称
1.环境变量,写在profile的变量
/etc/profile
~/.bash_profile
查看变量
#env 查看环境变量
#set 查看所有变量
#vim test4.sh ;chmod +x test4.sh
#!/bin/bash
echo $0 显示脚本的名称
echo $1 显示第一个参数
echo $2 显示第二个参数
echo $3 显示第三个参数
echo $* 显示所有参数
echo $# 统计参数有多少个
#./test4.sh a b c 1 8 7
++++++++++++++++++++++++
创建账户并修改密码
#vim test5.sh
#!/bin/bash
useradd $1
echo $2|passwd --stdin $1
#./test5.sh tom 123
+++++++++++++++++++++++++++++
#ls install.log
#echo $? 查看上一个命令执行的结果
返回0代表成功
非0代表失败
#echo $$ 返回当前进程的PID
++++++++++++++++++++++++++++
Shell中的引号
双引"" 引用整体
单引'' 引用整体,屏蔽特殊含义
反引`` 引用命令,取命令的执行结果
#a=12
#echo a b c != echo "a bc"
#echo "abc" == echoabc
#echo 'a b c' == echo "a bc"
#echo '13$a' != echo"13$a"
#echo "\$a" == echo'$a'
#tar -zcf log-`date +%Y%m%d`.tar.gz /var/log
#tar -zcf log-$(date +%Y%m%d).tar.gz /var/log
+++++++++++++++++++++++++++++
写脚本时定义变量,不赋值
运行脚本的人给变量赋值
read -p "提示符" 变量
+++++++++++++++++++++++++++++
修改制定用户的密码,用户名和密码由用户输入
#vim test6.sh
#!/bin/bash
read -p "请输入用户名:" user
stty -echo
read -p "请输入密码:" pass
stty echo
echo $pass |passwd --stdin $user
+++++++++++++++++++++++++++++
局部变量(仅当前有效的变量)
全局变量(当前及所有子进程都有效的变量)
#test=123 定义局部变量
#bash
#echo $test 无该变量
#exit
#export test=222 定义全局变量
#bash
#echo $test 变量存在且有值
#exit
#test=333 定义局部变量
#export test 转换为全局变量
#export -n test 取消全局变量