Bash脚本忽略通过文件中的空白行和注释

时间:2018-11-08 04:01:17

标签: bash scripting

#!/bin/bash
set -o nounset

##############################################################
# ASSIGNMENT DUE NOV m19/t20
# Allow for blank lines in the file
# Allow for comments in the file that start tih '#'
# Validate ssn with regex
# Log SIG, line numbers, filename, and bad ssn
# Validat domain with regex (lowercase and digits)
# Log SIG, line number, filename, and bad ip
# add domain name and ip to /etc/hosts only when required
# (Always use fullly qualified paths in bash scripts)
# regex files should be in /root/scripts/regex/
##############################################################

# Script to check for errors and then print to screen
args_expected=1
if [ ${#} -ne ${args_expected} ]
then
    echo "Usage error: expected ${args_expected}, got ${#}"
    echo "Exiting"
    exit
fi

IFS_save=$IFS
IFS=','

user_cnt=0

#Script to make usernames for the users in the file
while read lastname firstname midname ssn domain ip;
do
    username=${firstname:0:1}${midname:0:1}${lastname}
    echo "user_cnt=${user_cnt} username: $username"

    echo $lastname $firstname $midname $ssn $domain $ip;
    let ++user_cnt

    #Allows blank line within files
    if [ "${lastname}" = " " ]
    then
        #needs to skip over the blank line and not to count it as a user
    else
        continue
    fi
done < ${1}

IFS=$IFS_save
  1. 该程序还有很多要做的工作,但现在我需要有关注释和空白行的帮助。

  2. 每当我在示例文件中使用该程序时,我都会不断添加空白行作为用户。

  3. 我尝试使用grep -o awk -F和sed regex选项来删除/忽略空行,但是在形式上,我拥有if语句,它所做的就是读取空行并将其添加到用户计数中

1 个答案:

答案 0 :(得分:0)

在增加user_cnt之前,在 之前输入空白的用户名或注释。并在这种情况下使用continue,而不是非空白的情况。

" "不是空的用户名,而是包含空格字符的用户名。空字符串为""

#!/bin/bash
set -o nounset

##############################################################
# ASSIGNMENT DUE NOV m19/t20
# Allow for blank lines in the file
# Allow for comments in the file that start tih '#'
# Validate ssn with regex
# Log SIG, line numbers, filename, and bad ssn
# Validat domain with regex (lowercase and digits)
# Log SIG, line number, filename, and bad ip
# add domain name and ip to /etc/hosts only when required
# (Always use fullly qualified paths in bash scripts)
# regex files should be in /root/scripts/regex/
##############################################################

# Script to check for errors and then print to screen
args_expected=1
if [ ${#} -ne ${args_expected} ]
then
    echo "Usage error: expected ${args_expected}, got ${#}"
    echo "Exiting"
    exit
fi

IFS_save=$IFS
IFS=','

user_cnt=0

#Script to make usernames for the users in the file
while read lastname firstname midname ssn domain ip;
do
    # Check for blank or comment
    if [[ $lastname = "" || $lastname = "#"* ]]
    then
        continue
    fi

    username=${firstname:0:1}${midname:0:1}${lastname}
    echo "user_cnt=${user_cnt} username: $username"

    echo $lastname $firstname $midname $ssn $domain $ip;
    let ++user_cnt
done < ${1}

IFS=$IFS_save

我使用以下CSV文件进行了测试:

margolin,barry,w,104500034,margolin.com,1.2.3.4

#foobar
jones,james,e,123455667,foobar.com,1.1.1.1

输出为:

user_cnt=0 username: bwmargolin
margolin barry w 104500034 margolin.com 1.2.3.4
user_cnt=1 username: jejones
jones james e 123455667 foobar.com 1.1.1.1
相关问题