Bash Scripting Ignore blank lines + comments in a passed through file











up vote
-1
down vote

favorite












#!/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. This program has a lot left to it that needs to be done but right now I need help with the comments and blank lines.


  2. Whenever I use this program through the sample file I have it keeps adding blank lines as users.


  3. I have tried using grep -o awk -F and sed regex options in removing / ignoring blank lines but in the form I have the if statement all it does is read the blank lines and adds them to the user count.











share|improve this question
























  • Please edit your Q to include a small set of sample input and your expected output from that same input. Good luck.
    – shellter
    Nov 8 at 4:16










  • expected ${args_expected}, got ${#} is an error message. Error messages belong on stderr. And if your script fails, it should return non zero. echo "error message" >&2; exit 1
    – William Pursell
    Nov 8 at 4:40















up vote
-1
down vote

favorite












#!/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. This program has a lot left to it that needs to be done but right now I need help with the comments and blank lines.


  2. Whenever I use this program through the sample file I have it keeps adding blank lines as users.


  3. I have tried using grep -o awk -F and sed regex options in removing / ignoring blank lines but in the form I have the if statement all it does is read the blank lines and adds them to the user count.











share|improve this question
























  • Please edit your Q to include a small set of sample input and your expected output from that same input. Good luck.
    – shellter
    Nov 8 at 4:16










  • expected ${args_expected}, got ${#} is an error message. Error messages belong on stderr. And if your script fails, it should return non zero. echo "error message" >&2; exit 1
    – William Pursell
    Nov 8 at 4:40













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











#!/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. This program has a lot left to it that needs to be done but right now I need help with the comments and blank lines.


  2. Whenever I use this program through the sample file I have it keeps adding blank lines as users.


  3. I have tried using grep -o awk -F and sed regex options in removing / ignoring blank lines but in the form I have the if statement all it does is read the blank lines and adds them to the user count.











share|improve this question















#!/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. This program has a lot left to it that needs to be done but right now I need help with the comments and blank lines.


  2. Whenever I use this program through the sample file I have it keeps adding blank lines as users.


  3. I have tried using grep -o awk -F and sed regex options in removing / ignoring blank lines but in the form I have the if statement all it does is read the blank lines and adds them to the user count.








bash scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 4:14









Barmar

414k34239340




414k34239340










asked Nov 8 at 4:01









Drakke Kirkpatrick

11




11












  • Please edit your Q to include a small set of sample input and your expected output from that same input. Good luck.
    – shellter
    Nov 8 at 4:16










  • expected ${args_expected}, got ${#} is an error message. Error messages belong on stderr. And if your script fails, it should return non zero. echo "error message" >&2; exit 1
    – William Pursell
    Nov 8 at 4:40


















  • Please edit your Q to include a small set of sample input and your expected output from that same input. Good luck.
    – shellter
    Nov 8 at 4:16










  • expected ${args_expected}, got ${#} is an error message. Error messages belong on stderr. And if your script fails, it should return non zero. echo "error message" >&2; exit 1
    – William Pursell
    Nov 8 at 4:40
















Please edit your Q to include a small set of sample input and your expected output from that same input. Good luck.
– shellter
Nov 8 at 4:16




Please edit your Q to include a small set of sample input and your expected output from that same input. Good luck.
– shellter
Nov 8 at 4:16












expected ${args_expected}, got ${#} is an error message. Error messages belong on stderr. And if your script fails, it should return non zero. echo "error message" >&2; exit 1
– William Pursell
Nov 8 at 4:40




expected ${args_expected}, got ${#} is an error message. Error messages belong on stderr. And if your script fails, it should return non zero. echo "error message" >&2; exit 1
– William Pursell
Nov 8 at 4:40












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Put the check for a blank username or comment before you increment user_cnt. And use continue in that case, not the case where it's not blank.



And " " is not an empty username, it's a username containing a space character. The empty string is "".



#!/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


I tested with the following CSV file:



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

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


and the output was:



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





share|improve this answer























  • oh nvm I forgot to move the whole if statement above, thanks m8
    – Drakke Kirkpatrick
    Nov 8 at 4:36













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201379%2fbash-scripting-ignore-blank-lines-comments-in-a-passed-through-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Put the check for a blank username or comment before you increment user_cnt. And use continue in that case, not the case where it's not blank.



And " " is not an empty username, it's a username containing a space character. The empty string is "".



#!/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


I tested with the following CSV file:



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

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


and the output was:



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





share|improve this answer























  • oh nvm I forgot to move the whole if statement above, thanks m8
    – Drakke Kirkpatrick
    Nov 8 at 4:36

















up vote
0
down vote



accepted










Put the check for a blank username or comment before you increment user_cnt. And use continue in that case, not the case where it's not blank.



And " " is not an empty username, it's a username containing a space character. The empty string is "".



#!/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


I tested with the following CSV file:



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

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


and the output was:



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





share|improve this answer























  • oh nvm I forgot to move the whole if statement above, thanks m8
    – Drakke Kirkpatrick
    Nov 8 at 4:36















up vote
0
down vote



accepted







up vote
0
down vote



accepted






Put the check for a blank username or comment before you increment user_cnt. And use continue in that case, not the case where it's not blank.



And " " is not an empty username, it's a username containing a space character. The empty string is "".



#!/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


I tested with the following CSV file:



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

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


and the output was:



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





share|improve this answer














Put the check for a blank username or comment before you increment user_cnt. And use continue in that case, not the case where it's not blank.



And " " is not an empty username, it's a username containing a space character. The empty string is "".



#!/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


I tested with the following CSV file:



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

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


and the output was:



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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 4:42

























answered Nov 8 at 4:21









Barmar

414k34239340




414k34239340












  • oh nvm I forgot to move the whole if statement above, thanks m8
    – Drakke Kirkpatrick
    Nov 8 at 4:36




















  • oh nvm I forgot to move the whole if statement above, thanks m8
    – Drakke Kirkpatrick
    Nov 8 at 4:36


















oh nvm I forgot to move the whole if statement above, thanks m8
– Drakke Kirkpatrick
Nov 8 at 4:36






oh nvm I forgot to move the whole if statement above, thanks m8
– Drakke Kirkpatrick
Nov 8 at 4:36




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201379%2fbash-scripting-ignore-blank-lines-comments-in-a-passed-through-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini