• Linux SHELL 命令入门题目答案(一)


    1、如何使用shell 打印 “Hello World!”

    (1)如果你希望打印 !,那就不要将其放入双引号中,或者你可以通过转义字符转义
    (2)echo 'hello world!' 使用单引号echo 时,bash 不会对单引号中的变量求值

    2、求变量的长度

    var='hello world'
    echo ${#var} \ 11

    3、$0 表示 SHELL 的名称,是那种SHELL 一般为 bash $SHELL 为SHELL 的位置 /bin/bash

    4、完成定义2个变量 no1=1,no2=2 no3=no1+no3 ,然后打印 no3

    no1=2
    no2=3
    let no3=no1+no2
    echo $no3

    如果是 no3=$no1+$no2 会输出什么?

    5、完成判断是否为root用户的SHELL 命令

    if [ $UID -ne 0 ]; then echo 'not root!'; else echo 'root!' ;fi

    #if 中括号 杠相不相等,分号 then 直接跟语句 fi

    6、shell 进行浮点数运算 1.5*4

    var1=1.5
    var2=4
    result=`echo "$var1*$var2" | bc`

    7、执行某条命令cmd,将正确及错误信息都输入到 output.txt

    cmd output.txt 2>&1 

    8、使用写一个脚本,运行时能够将下面的内容放入 aa.txt

    wget http://localhost:8080/obu-interface/index.jsp
    netstat -anp | grep 8080

    #!/bin/bash
    cat <<EOF>aa.txt
    wget http://localhost:8080/obu-interface/index.jsp
    netstat -anp | grep 8080
    EOF

    9、定义一个含有4个元素的一维数组,并依次完成,打印其中的第2个元素,打印数组长度,打印所有元素

    array_var=(1 2 3 4)
    echo ${array_var[1]}
    echo ${array_var[*]} 或 echo ${array_var[@]}
    echo ${#array_var[*]} 或 echo ${#array_var[@]}

    10、打印文件下所有的文本,非目录

    for file in `ls` ;
    do
    if [ -f $file ];
    then
    echo $file;
    fi
    done

    11、按照这种格式输出时间

    2016-01-13 18:06:07 date '+%Y%m%d%H%M%S'

    20160113180750 date '+%Y-%m-%d %T'

    1452679772 date '+%s'

    12、while 循环 10次 依次在屏幕的一行内打印 1-10,打印一次休眠1s

    #!/bin/bash
    echo -n Count:
    count=0;
    while true;
    tput sc; #存储光标位置
    do
    if [ $count -lt 4 ] ; then
    sleep 1;
    let count++;
    tput rc; #恢复关标位置
    tput ed; #清空光标后的内容
    echo -n $count;
    else
    exit 0;
    fi

    done;

    13、写一断代码检测一个命令 cmd 是否执行成功

    #!/bin/bash
    CMD="ls -l"
    $CMD
    if [ $? -eq 0 ] ; then
    echo "$CMD executed successfully"
    else
    echo "$CMD executed failed"
    fi

    14、以下文件系统相关判断分别是什么含义

    [ -f $file ]

    [ -d $file ]

    [ -e $file ]

    [ -w $file ]

    [ -r $file ]

    [ -x $file ]

    15、SHELL 如何判断两个字符串是否相等、字符串比较大小、字符串是否为空

    if [[ $str1 = $str2 ]]

    if [[ $str1 > $str2 ]]

    if [[ -z $str1 ]] #str1 为空 返回真

    if [[ -n $str1 ]] #str1 非空 返回真

    16、cat 显示行号 cat -n test.txt

    17、使用 find 命令 将某个文件夹下的所有txt 文件全部找到,并删除、备份(或 拷贝到另一个目录下),分别使用 -exec xargs 等命令

    find /i -type f -name "*.txt" -exec cp {} /test ;
    find /i -type f -name "*.txt" -exec rm -rf {} ;

    find /i -type f -name "*.txt" -print | xargs rm -rf ;

    find /i -type f -name "*.txt" -print | xargs -t -i cp {} {}.bak

    # 大多数 Linux 命令都会产生输出:文件列表、字符串列表等。但如果要使用其他某个命令并将前一个命令的输出作为参数该怎么办?例如,file 命令显示文件类型(可执行文件、ascii 文本等);你能处理输出,使其仅显示文件名,目前你希望将这些名称传递给 ls -l 命令以查看时间戳记。xargs 命令就是用来完成此项工作的。他允许你对输出执行其他某些命令。

    # -i 选项告诉 xargs 用每项的名称替换 {}

    18、使用tr 命令 将 HELLO WORLD 替换成小写

    echo "HELLO WORLD" | tr 'A-Z' 'a-z'

    19、替换文件 text.txt 的所用 制表符为空格

    cat text.txt | tr ' ' ''

    20、使用 tr 命令 删除数字

    echo 'hello 124 world 345!'| tr -d '0-9' #-d 表示删除

    21、删除Windows文件“造成”的'^M'字符

    cat file | tr -s " " " " > new_file # -s 表示替换重复的字符串

  • 相关阅读:
    序列化
    gdb core dump调试
    QT下使用QCustomPlot绘制曲线
    windows、linux通过ftp从ftp服务器上传和下载
    html、java、mysql数据交互之数据分页显示
    html、java、mysql数据交互
    使用纯C函数指针调用C++的类成员函数
    设计模式指六-Adapter模式
    设计模式之五-Facade模式
    设计模式之四-Factory模式
  • 原文地址:https://www.cnblogs.com/oftenlin/p/5129285.html
Copyright © 2020-2023  润新知