zoukankan      html  css  js  c++  java
  • shell学习(一)

    一、免密交互

    --stdin

     1、从标准输入读取字符串

    如:passwd   --stdin   heruguo246

    [root@localhost mnt]# passwd --stdin heruiguo246
    Changing password for user heruiguo246.
    123456    ---输入了修改heruiguo246用户密码为123456
    passwd: all authentication tokens updated successfully.

    2、可以从键盘,也可以从另一个命令给出

    如:echo  1234567 |passwd  --stdin   herugiu246

    [root@localhost mnt]# echo 1234567 | passwd --stdin heruiguo246
    Changing password for user heruiguo246.
    passwd: all authentication tokens updated successfully.

    这一次就没有在手动输入密码了,完全脚本实现。

    二、忽略无关输出

    黑洞设备/dev/null

    只能写入,不能读出的单向文件,存放到其中的数据都会丢失。

    用法:可执行语句  &>/dev/null

    echo   1234567|passwd  --stdin  heruiguo246 &>/dev/null

    [root@localhost mnt]# echo 1234567|passwd --stdin heruiguo246 &>/dev/null
    [root@localhost mnt]#

    注意:&和>以及>和/dev/null之间没有空格,否则要报错

    三、记录错误信息

    用法:可执行语句    2>/路径/日志文件名

    如:sh  /mnt/adduser.sh  2>/mnt/adderror.log

    四、逻辑分割

    1、||  逻辑关系为“或者”,任何一条命令执行成功都符合期望,只有在前面的命令执行失败时,后面的命令才会执行。

    如:id   test  || useradd   test    --表示当test用户不存在时,创建一个用户。

    五、双引号和单引号的区别

    双引号:

    (1)在双引号中可以用$扩展,来表示变量,如:

    [root@localhost mnt]# a=5
    [root@localhost mnt]# echo "你的值是:$a"
    你的值是:5

    (2)出现特殊字符时,可以用来表示转义, 表示制表符、 表示换行符,如:

    [root@localhost mnt]# a="a b c d e f g h"
    [root@localhost mnt]#
    [root@localhost mnt]# echo -e $a     -e参数表示解析特殊转义符
    a  b  c  d
    e  f   g  h

    (3)当变量值不包括空格、制表符、双引号通常被省略,如:

    [root@localhost mnt]# a=centos6.5
    [root@localhost mnt]# b=$a server
    -bash: server: command not found
    [root@localhost mnt]# b="$a server"
    [root@localhost mnt]# echo $b
    centos6.5 server

    单引号:

    (1)所有字符串均视为字符本身(无特殊)如:

    [root@localhost mnt]# a=centos
    [root@localhost mnt]# echo '$a'
    $a
    [root@localhost mnt]#

    (2)不允许转义

    六、read取值的用法

    基本格式

    read   变量名

    read   -p  “提示信息”    变量名

    [root@localhost mnt]# read name
    123
    [root@localhost mnt]# echo $name
    123
    [root@localhost mnt]#
    [root@localhost mnt]# read -p "请输入用户名:" name
    请输入用户名:xiaoming
    [root@localhost mnt]# echo $name
    xiaoming

    静默取值加-s在输入密码时不显示在屏幕上

    [root@localhost mnt]# read -s -p "请输入密码:" passwd
    请输入密码:
    [root@localhost mnt]# echo $passwd
    123456

  • 相关阅读:
    第四章 基础命令的学习
    7-7命令总结
    第三章 虚拟机的简单使用及其xshell远程工具的使用
    第二章 虚拟机的软件及其安装
    在VMware中安装CentOS系统步骤
    Pytest02-用法和调用
    Pytest01-从官方文档开始学习
    浅谈mock技术
    C/C++ malloc、calloc、realloc和alloca
    Go 知识汇总
  • 原文地址:https://www.cnblogs.com/heruiguo/p/8384377.html
Copyright © 2011-2022 走看看