zoukankan      html  css  js  c++  java
  • 适用于Bash编程初学者小例子

    去掉字符串前面的一个或多个空格。

    #!/bin/bash


    str="f810yunlong-3:     Cache flushing complete.      "

    IFS=':' read -ra substrs <<< "$str"
    NODE="${substrs[0]}"
    MSG="${substrs[1]}"


    TRIM_MSG= echo "$MSG" | sed -e 's/^[[:space:]]*//'


    #Remove the leading spaces from the variable
    TRIM_MSG=`echo $MSG | sed -e 's/^[[:space:]]*//'`


    # Print the value of $Var after trimming
    printf "%s " "$NODE"
    printf "=%s= " "$TRIM_MSG"


    判断一个字符串里是否包含有一个子字符串。

    #!/bin/bash


    str="f810yunlong-3: Cache flushing complete.    "


    if [[ "$str" == *"Cache flushing complete."* ]]
    then
         printf "One node cache flushed successfully! "
    else
         printf "[Error] "
         printf "Error happened in one node on flushing inline dedupe index and queue flush! "
    fi


    遍历一个文件夹里的所有的zip文件,以各自的zip文件名为各自的目录名,逐个解压到指定的目录下。

    #!/bin/sh

    ZIPDIR="/ifs/yunlong_bash/dir_zips"
    UNZIPDIR="/ifs/yunlong_bash/dir5"


    printf "Entered path: $ZIPDIR. "
    cd "$ZIPDIR"


    for zip in *.zip
    do
       dirname=`echo $zip | sed 's/.zip$//'`
       printf "Directory name to extract this file is: %s. " $dirname
       dirfullpath="$UNZIPDIR/$dirname"
       printf "Directory full path to extract this file is:%s. " $dirfullpath
      
       mkdir "$dirfullpath"
       unzip $zip -d $dirfullpath
       printf " "
      
    done

    经过检验成功!


    遍历一个文件夹里所有的tar文件,以各自的文件名为目录名,逐个解压到指定的目录下。

    #!/bin/sh


    TARDIR="/ifs/yunlong_bash/tar_test/tars"
    UNTARDIR="/ifs/yunlong_bash/tar_test/untars"


    printf "Entered path: $TARDIR. "
    cd "$TARDIR"


    for tar in *.tar
    do
       dirname=`echo $tar | sed 's/.tar$//'`
       printf "Directory name to extract this file is: %s. " $dirname
       dirfullpath="$UNTARDIR/$dirname"
       printf "Directory full path to extract this file is:%s. " $dirfullpath
      
       mkdir "$dirfullpath"
       tar -xvf $tar -C $dirfullpath

      printf " "
      
    done

    经笔者亲自运行检验通过。


    终止脚本的进一步执行。

    #!/bin/bash

    printf "Script is executing."
    exit
    printf "Script is still executing after exit."

    *一试便知,第二句的打印终端上看不到的,因为没被执行。


    逐行读取一个文本文件,并将内容输出。

    #!/bin/bash
    filename="/home/yunlong/source_datasets.txt"
    n=1

    while read line; do
    # reading each line
    echo "Line No. $n : $line"
    n=$((n+1))
    done < $filename


    参考资料

    ===========

    https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash

    https://linuxhint.com/trim_string_bash/

    https://bash.cyberciti.biz/guide/Exit_command

    https://linuxhint.com/read_file_line_by_line_bash/

  • 相关阅读:
    hdu 4825 Xor Sum (01 Trie)
    hdu 5877 Weak Pair (Treap)
    bzoj 1861: [Zjoi2006]Book 书架 (splay)
    bzoj 1503: [NOI2004]郁闷的出纳员 (splay)
    hihocoder#1333 : 平衡树·Splay2 (区间操作)
    「BZOJ1251」序列终结者 (splay 区间操作)
    二进制运算符的相关运算
    Bzoj 1085: [SCOI2005]骑士精神 (dfs)
    Bzoj 1083: [SCOI2005]繁忙的都市 (最小生成树)
    Bzoj 1088: [SCOI2005]扫雷Mine (DP)
  • 原文地址:https://www.cnblogs.com/awpatp/p/13266397.html
Copyright © 2011-2022 走看看