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
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.
Whenever I use this program through the sample file I have it keeps adding blank lines as users.
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
add a comment |
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
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.
Whenever I use this program through the sample file I have it keeps adding blank lines as users.
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
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
add a comment |
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
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.
Whenever I use this program through the sample file I have it keeps adding blank lines as users.
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
#!/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
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.
Whenever I use this program through the sample file I have it keeps adding blank lines as users.
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
bash scripting
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
add a comment |
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
add a comment |
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
oh nvm I forgot to move the whole if statement above, thanks m8
– Drakke Kirkpatrick
Nov 8 at 4:36
add a comment |
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
oh nvm I forgot to move the whole if statement above, thanks m8
– Drakke Kirkpatrick
Nov 8 at 4:36
add a comment |
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
oh nvm I forgot to move the whole if statement above, thanks m8
– Drakke Kirkpatrick
Nov 8 at 4:36
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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