几分钟带你了解shell基础命令

一、shell 的功能:

                    Shell别称命令解释器,它能识别用户输入的各种命令,并传递给操作系统。类似于Windows操作系统的命令行。同时也是控制系统的脚本语言。

练习题:

1.命令使用:echo、eval、exec、export、read、shift、exit(要求退出码为222) 这些命令的功能,以及用例

①echo命令:

     echo可在屏幕上输出信息

    echo    -n     不换行输出内容 

例如:

[root@localhost test]# echo -n we need  success
we need success[root@localhost test]#

    echo     -e   解析转义字符        (实际意义就是紧跟其后的字符取字面值)

例如:

[root@localhost test]# echo  -e we\tneed\tsuccess
wetneedtsuccess

[root@localhost test]# echo -e "just\tdo\tit"
just    do    it
[root@localhost test]# echo -e "just\ndo\nit"
just
do
it
 

转义字符还有:

   \n       换行   \r       回车      \t      制表符   \b     退格   \v        纵向制表符

②eval 命令:   

[root@localhost test]# sysname='shuju;head -1 /etc/passwd'
[root@localhost test]# echo  $sysname
shuju;head -1 /etc/passwd

[root@localhost test]# eval echo $sysname
shuju
root:x:0:0:root:/root:/bin/bash
功能:当shell程序执行到eval语句时,shell读入参数args(复数),组合成为一个新的命令,再次执行。

③exec命令:

[root@localhost ~]# cd /etc
[root@localhost etc]# ssh root@192.168.184.128
[root@localhost ~]# exec cd /home
Connection to 192.168.184.128 closed.
[root@localhost etc]# 
 

功能:够在不创建新的子进程的前提下,转到执行指定的命令,当指定的命令执行完毕后,进程就就终止了。

④export命令:设置或者显示环境变量。

[root@localhost test]# zhangsan="this is test";export zhangsan
[root@localhost test]# env | grep zhangsan
zhangsan=this is test
 

⑤read命令:

[root@localhost test]# read  -t 8 -p "please input your password:" password 
please input your password:zhangsan
[root@localhost test]# echo $password
zhangsan

[root@localhost test]# echo -n "please input your name :"; read name1 name2
please input your name :zhangsan lisi  
[root@localhost test]# echo $name1
zhang  san
[root@localhost test]# echo $name2
lisi

利用指定分隔符进行分隔 实现包含空格

[root@localhost test]# echo -a "please input your name :";IFS=";" read name1 name2
-a please input your name :
ZHANG HANG;LISI
[root@localhost test]# echo $name1
ZHANG HANG

功能:从标准输入读取字符串等信息,传给shell程序内部定义的变量。

-p prompt:设置提示信息         -t timeout:设置输入等待时间,单位默认为秒

一般都是简写(-p     -t     -n)

⑥shift命令:

[root@localhost test]# vim shitf_test.sh
until [ $# -eq 0 ]
do
        echo "第一个参数为:$1 参数个数为:$#"
shift
done
[root@localhost test]# bash shitf_test.sh 1 2 3 4 5 6
第一个参数为:1 参数个数为:6
第一个参数为:2 参数个数为:5
第一个参数为:3 参数个数为:4
第一个参数为:4 参数个数为:3
第一个参数为:5 参数个数为:2
第一个参数为:6 参数个数为:1
 

功能:

  在程序中每一次使用shift语句,都会使所有的位置参数依次向左移动一个位置,并使位置参

数$#  减一,直到减到0为止。

⑦exit命令:要求退出码为222

[root@localhost test]# vim exit_test.sh

    echo "hello world"

    exit 222

[root@localhost test]# bash exit_test.sh 
hello world
[root@localhost test]# echo $?
222

3.定义自定义环境变量:(使用export和declare)

  

    针对root用户的所有连接:root_data=root

[root@localhost test]# vim /etc/profile       #针对root用户的所有连接: root_data=root

ROOT_DATA="ROOT"
export ROOT_DATA
 

针对所有用户的变量: all_data=all

[root@localhost test]# vim ~/.bash_profile
ALL_DATA=="ALL"
export ALL_DATA

重新加载环境变量:

[root@localhost test]# source ~/.bash_profile

[root@localhost test]# source ~/.etc/profile

  查看所有的环境变量, 查看所有变量

[root@localhost test]# set | grep ROOT_DATA
ROOT_DATA=ROOT
[root@localhost test]# set | grep ALL_DATA
ALL_DATA==ALL

4.脚本执行的方式及特点:用样例验证

[root@localhost test]# bash year.sh      # bash 产生子进程再运行,使用指定的bash shell去运行。
请输入一个年份:2008
2008 是闰年
 

5.设置vim编辑sh文件的缩进为4个空格

  1. [root@localhost test]# vim ~/.vimrc

  2. autocmd FileType sh setlocal ai ts=4 sw=4 et

6.$#,$*,$@,$n,$?的使用

[root@localhost test]# vim a_test.sh

echo "*********"    
echo $#                            参数总数
echo "*********"
echo $2                            第二个数的值
echo "*********"
echo $?                            0表示成功      执行状态码

[root@localhost test]# bash a_test.sh  1 2 3 
*********
3
*********
2
*********
0

$*   以"参数1 参数2 参数3…"的形式返回所有参数的值

$@   以"参数1""参数2""参数3"…的形式返回所有的值

例如:

[root@localhost test]# vim return_all_var_index.sh

for i in "n1  n2 n3 n4 n5"
do
 echo $i
done

for i in "z1" "z2" "z3" "z4" "z5"
do
 echo $i
done

结果如下:

[root@localhost test]# bash return_all_var_index.sh 
n1 n2 n3 n4 n5
z1
z2
z3
z4
z5
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值