• Shell 中字符串变量的赋值注意点


    1. 变量赋值

    语法:var="saaaa"

    PS: 等号两边不能有空格

    2. 脚本示例如下:

    
    #!/bin/sh
    # Get bug activity info
    # usage get_bug_activity <bug_id_list_file>
    
    if [ $# -lt 2 ]; then
      echo "Usage:";
      echo "$0 <blk_pair_id_list_file> <dir>";
      exit;
    fi
    
    if [ ! -d $2 ]; then
    	echo "make DIR $2";
    	mkdir $2;
    fi
    
    echo -e "processing bug list file 33[1;31m$133[m";
    
    n=0
    m=0
    	cat $1 | while read line                                   # read file $1 line by line
    	do
    		BugID=`echo ${line%%[A-Z]*} | sed  's/[ 	]*$//g'`;  # get Bugid from each line $line, 
    		                                                                         # and remove the space of the string tail
    		                                                                         # PS: BugID="***" right; BugID = "***" wrong
    		                                                                         # There should no space between "=
    		let n=n+1;
    		echo -e "33[1;35m$n33[m $BugID";
    		rfile="$2/a$BugID.htm";
            if [ ! -f "$rfile" ]; then
    				curl "https://bugs.eclipse.org/bugs/show_activity.cgi?id=$BugID" -o "$rfile";
    		else
    				let m=m+1;
    				echo -e "33[1;31mfile existed...33[m $m duplicated";
    		fi
    	done
    
    echo "finished";
    
    

    3. 示例讲解

    1. echo -e "processing bug list file 33[1;31m$133[m"; : 用来设置变量$1 的颜色。 33[1;31m 红色文字 33[m .
    2. 逐行读取文件内容到 $line
        cat $1 | while read line                                   # read file $1 line by line
        do
                ## do something
        done
    
    1. 将读到的内容只截取第一个字段 (后面可能会有一些空格): echo ${line%%[A-Z]*}.
    2. 用sed 删除字符串最后的空格 :echo ${line%%[A-Z]*} | sed 's/[ ]*$//g
    3. 访问网址,并输出到文件 $rfile
      curl "https://bugs.eclipse.org/bugs/show_activity.cgi?id=$BugID" -o "$rfile"
  • 相关阅读:
    java中的异常类
    Mysql--JDBC的基础
    eclipse使用断言
    idea中使用断言
    java的null
    array,集合(collection),集合(list)的区别
    命名管道FIFO
    标准库中的管道操作
    现代进程间的通信方式--管道
    广播编程之发送者
  • 原文地址:https://www.cnblogs.com/juking/p/8885057.html
Copyright © 2020-2023  润新知