RHEL: syntax errors with bash script











up vote
-1
down vote

favorite












I was tasked to do a bash script that can audit logins in mysql.


If you activate the general_log option in /etc/my.cnf, you can register all the activity that mysql does and it get write (in my case) in /var/lib/mysql/localhost.log.
If i cat it, i get this line:



2018-11-18T12:39:46.622298Z     5 Connect   Access denied for user 'root'@'localhost' (using password: YES)


So, with that in mind, I made this grep syntax (change the variables to actual numbers and it works!):



grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied




grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied


I also made a grep instruction that counts how many of these logins are made.



grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied




grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied


They all work and, AFAIK they are technically correct (spellchecked!).



But I need to bake them into a script so I can take variables from the user and put them into a proper script, but I can't make them work so far: I'm getting the error you can see below when bash tries to read the first variable.



The code so far:



#!/bin/bash
echo "Year input: "
read -r year
if [[ $year -gt 2020 ]];
then
echo "Incorrect year input."
exit 1
else
echo "Month input: "
read -r month
if [[ $month -gt 12 ]];
then
echo "Incorrect month input."
exit 1
else
echo "Day input: "
read -r day
if [[ $day -gt 31 ]];
then
echo "Incorrect day input."
exit 1
else
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
logbien=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
echo "Correct logins: "echo "$logbien" | wc -1 " times."
echo ''
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied
logmal=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
echo "Incorrect logins: "echo "$logmal" | wc -1 " times."
fi
fi
fi
exit 0


The error I get (original: image 1 below):



[root@localhost ~]# sh /medla/sf_compartida/two.sh
Year input:
2018
': not a valid identifiersh: line 3: read: `year
/media/sf_compartida/two.s: line 34: syntax error: unexpected end of file
[root@localhost ~]#


Edit



New code:



#!/bin/bash
read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d")
then
echo "Error: invalid date" >&2
exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]
then
echo "invalid year" >&2
exit 1
else
grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
logbien=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
echo "Correct logins: "echo "$logbien" | wc -1" times."
echo ''
grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep denied
logmal=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
echo "Incorrect logins: "echo "$logmal" | wc -1" times."
fi


The error I get (original: image 2 below):



[root@localhost ~]# sh /media/sf_compartida/two2.sh
Enter the date (YYYY-mm-dd): 2018-11-20
': not a valid identifier.sh: line 2: read: `date
/media/sf compartida/two2.sh: line 9: syntax error in conditional expression
'media/sf_compartida/two2.sh: line 9: syntax error near `]]
'media/sf_compartida/two2.sh: line 9: `if [[ $year -gt 2020 ]]
[root@localhost ~]#




Error 1 - image:
enter image description here



Error 2 - image:
enter image description here










share|improve this question









New contributor




jfalava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3




    shellcheck.net is a good resource for syntax checking shell scripts. You don't have any glaring errors here: you could improve your quoting and command substitution (as pointed out by shellcheck.net). The main problem with the output is that logbien and logmal contain the grep output which is newline-separated lines. If you want the number of good logins, you need to count those lines: n_bien=$(echo "$logbien" | wc -l)
    – glenn jackman
    Nov 25 at 13:58










  • You may also want to run your script as (export LC_MESSAGES=C; ./your_script) to report the actual error messages in English.
    – fra-san
    Nov 25 at 14:06










  • I hope you don't have a user called "denied" :-)
    – Kusalananda
    Nov 25 at 14:15






  • 1




    The : not an identifier lines are the key. You have a carriage return character at the end of one or more of your read lines. Get rid of them in an editor. They might be displayed as ^M.
    – Mark Plotnick
    Nov 25 at 21:11






  • 1




    Looks like you definitely have CR (r) in there. Remove that semicolon, then run dos2unix < two.sh > twofixed.sh and see if that new script still has those problems.
    – Mark Plotnick
    Nov 25 at 23:46

















up vote
-1
down vote

favorite












I was tasked to do a bash script that can audit logins in mysql.


If you activate the general_log option in /etc/my.cnf, you can register all the activity that mysql does and it get write (in my case) in /var/lib/mysql/localhost.log.
If i cat it, i get this line:



2018-11-18T12:39:46.622298Z     5 Connect   Access denied for user 'root'@'localhost' (using password: YES)


So, with that in mind, I made this grep syntax (change the variables to actual numbers and it works!):



grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied




grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied


I also made a grep instruction that counts how many of these logins are made.



grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied




grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied


They all work and, AFAIK they are technically correct (spellchecked!).



But I need to bake them into a script so I can take variables from the user and put them into a proper script, but I can't make them work so far: I'm getting the error you can see below when bash tries to read the first variable.



The code so far:



#!/bin/bash
echo "Year input: "
read -r year
if [[ $year -gt 2020 ]];
then
echo "Incorrect year input."
exit 1
else
echo "Month input: "
read -r month
if [[ $month -gt 12 ]];
then
echo "Incorrect month input."
exit 1
else
echo "Day input: "
read -r day
if [[ $day -gt 31 ]];
then
echo "Incorrect day input."
exit 1
else
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
logbien=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
echo "Correct logins: "echo "$logbien" | wc -1 " times."
echo ''
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied
logmal=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
echo "Incorrect logins: "echo "$logmal" | wc -1 " times."
fi
fi
fi
exit 0


The error I get (original: image 1 below):



[root@localhost ~]# sh /medla/sf_compartida/two.sh
Year input:
2018
': not a valid identifiersh: line 3: read: `year
/media/sf_compartida/two.s: line 34: syntax error: unexpected end of file
[root@localhost ~]#


Edit



New code:



#!/bin/bash
read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d")
then
echo "Error: invalid date" >&2
exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]
then
echo "invalid year" >&2
exit 1
else
grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
logbien=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
echo "Correct logins: "echo "$logbien" | wc -1" times."
echo ''
grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep denied
logmal=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
echo "Incorrect logins: "echo "$logmal" | wc -1" times."
fi


The error I get (original: image 2 below):



[root@localhost ~]# sh /media/sf_compartida/two2.sh
Enter the date (YYYY-mm-dd): 2018-11-20
': not a valid identifier.sh: line 2: read: `date
/media/sf compartida/two2.sh: line 9: syntax error in conditional expression
'media/sf_compartida/two2.sh: line 9: syntax error near `]]
'media/sf_compartida/two2.sh: line 9: `if [[ $year -gt 2020 ]]
[root@localhost ~]#




Error 1 - image:
enter image description here



Error 2 - image:
enter image description here










share|improve this question









New contributor




jfalava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3




    shellcheck.net is a good resource for syntax checking shell scripts. You don't have any glaring errors here: you could improve your quoting and command substitution (as pointed out by shellcheck.net). The main problem with the output is that logbien and logmal contain the grep output which is newline-separated lines. If you want the number of good logins, you need to count those lines: n_bien=$(echo "$logbien" | wc -l)
    – glenn jackman
    Nov 25 at 13:58










  • You may also want to run your script as (export LC_MESSAGES=C; ./your_script) to report the actual error messages in English.
    – fra-san
    Nov 25 at 14:06










  • I hope you don't have a user called "denied" :-)
    – Kusalananda
    Nov 25 at 14:15






  • 1




    The : not an identifier lines are the key. You have a carriage return character at the end of one or more of your read lines. Get rid of them in an editor. They might be displayed as ^M.
    – Mark Plotnick
    Nov 25 at 21:11






  • 1




    Looks like you definitely have CR (r) in there. Remove that semicolon, then run dos2unix < two.sh > twofixed.sh and see if that new script still has those problems.
    – Mark Plotnick
    Nov 25 at 23:46















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I was tasked to do a bash script that can audit logins in mysql.


If you activate the general_log option in /etc/my.cnf, you can register all the activity that mysql does and it get write (in my case) in /var/lib/mysql/localhost.log.
If i cat it, i get this line:



2018-11-18T12:39:46.622298Z     5 Connect   Access denied for user 'root'@'localhost' (using password: YES)


So, with that in mind, I made this grep syntax (change the variables to actual numbers and it works!):



grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied




grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied


I also made a grep instruction that counts how many of these logins are made.



grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied




grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied


They all work and, AFAIK they are technically correct (spellchecked!).



But I need to bake them into a script so I can take variables from the user and put them into a proper script, but I can't make them work so far: I'm getting the error you can see below when bash tries to read the first variable.



The code so far:



#!/bin/bash
echo "Year input: "
read -r year
if [[ $year -gt 2020 ]];
then
echo "Incorrect year input."
exit 1
else
echo "Month input: "
read -r month
if [[ $month -gt 12 ]];
then
echo "Incorrect month input."
exit 1
else
echo "Day input: "
read -r day
if [[ $day -gt 31 ]];
then
echo "Incorrect day input."
exit 1
else
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
logbien=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
echo "Correct logins: "echo "$logbien" | wc -1 " times."
echo ''
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied
logmal=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
echo "Incorrect logins: "echo "$logmal" | wc -1 " times."
fi
fi
fi
exit 0


The error I get (original: image 1 below):



[root@localhost ~]# sh /medla/sf_compartida/two.sh
Year input:
2018
': not a valid identifiersh: line 3: read: `year
/media/sf_compartida/two.s: line 34: syntax error: unexpected end of file
[root@localhost ~]#


Edit



New code:



#!/bin/bash
read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d")
then
echo "Error: invalid date" >&2
exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]
then
echo "invalid year" >&2
exit 1
else
grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
logbien=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
echo "Correct logins: "echo "$logbien" | wc -1" times."
echo ''
grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep denied
logmal=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
echo "Incorrect logins: "echo "$logmal" | wc -1" times."
fi


The error I get (original: image 2 below):



[root@localhost ~]# sh /media/sf_compartida/two2.sh
Enter the date (YYYY-mm-dd): 2018-11-20
': not a valid identifier.sh: line 2: read: `date
/media/sf compartida/two2.sh: line 9: syntax error in conditional expression
'media/sf_compartida/two2.sh: line 9: syntax error near `]]
'media/sf_compartida/two2.sh: line 9: `if [[ $year -gt 2020 ]]
[root@localhost ~]#




Error 1 - image:
enter image description here



Error 2 - image:
enter image description here










share|improve this question









New contributor




jfalava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I was tasked to do a bash script that can audit logins in mysql.


If you activate the general_log option in /etc/my.cnf, you can register all the activity that mysql does and it get write (in my case) in /var/lib/mysql/localhost.log.
If i cat it, i get this line:



2018-11-18T12:39:46.622298Z     5 Connect   Access denied for user 'root'@'localhost' (using password: YES)


So, with that in mind, I made this grep syntax (change the variables to actual numbers and it works!):



grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied




grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied


I also made a grep instruction that counts how many of these logins are made.



grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied




grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied


They all work and, AFAIK they are technically correct (spellchecked!).



But I need to bake them into a script so I can take variables from the user and put them into a proper script, but I can't make them work so far: I'm getting the error you can see below when bash tries to read the first variable.



The code so far:



#!/bin/bash
echo "Year input: "
read -r year
if [[ $year -gt 2020 ]];
then
echo "Incorrect year input."
exit 1
else
echo "Month input: "
read -r month
if [[ $month -gt 12 ]];
then
echo "Incorrect month input."
exit 1
else
echo "Day input: "
read -r day
if [[ $day -gt 31 ]];
then
echo "Incorrect day input."
exit 1
else
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
logbien=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
echo "Correct logins: "echo "$logbien" | wc -1 " times."
echo ''
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied
logmal=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
echo "Incorrect logins: "echo "$logmal" | wc -1 " times."
fi
fi
fi
exit 0


The error I get (original: image 1 below):



[root@localhost ~]# sh /medla/sf_compartida/two.sh
Year input:
2018
': not a valid identifiersh: line 3: read: `year
/media/sf_compartida/two.s: line 34: syntax error: unexpected end of file
[root@localhost ~]#


Edit



New code:



#!/bin/bash
read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d")
then
echo "Error: invalid date" >&2
exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]
then
echo "invalid year" >&2
exit 1
else
grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
logbien=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
echo "Correct logins: "echo "$logbien" | wc -1" times."
echo ''
grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep denied
logmal=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
echo "Incorrect logins: "echo "$logmal" | wc -1" times."
fi


The error I get (original: image 2 below):



[root@localhost ~]# sh /media/sf_compartida/two2.sh
Enter the date (YYYY-mm-dd): 2018-11-20
': not a valid identifier.sh: line 2: read: `date
/media/sf compartida/two2.sh: line 9: syntax error in conditional expression
'media/sf_compartida/two2.sh: line 9: syntax error near `]]
'media/sf_compartida/two2.sh: line 9: `if [[ $year -gt 2020 ]]
[root@localhost ~]#




Error 1 - image:
enter image description here



Error 2 - image:
enter image description here







linux bash scripting login mysql






share|improve this question









New contributor




jfalava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




jfalava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









fra-san

969214




969214






New contributor




jfalava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 25 at 13:45









jfalava

13




13




New contributor




jfalava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





jfalava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






jfalava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 3




    shellcheck.net is a good resource for syntax checking shell scripts. You don't have any glaring errors here: you could improve your quoting and command substitution (as pointed out by shellcheck.net). The main problem with the output is that logbien and logmal contain the grep output which is newline-separated lines. If you want the number of good logins, you need to count those lines: n_bien=$(echo "$logbien" | wc -l)
    – glenn jackman
    Nov 25 at 13:58










  • You may also want to run your script as (export LC_MESSAGES=C; ./your_script) to report the actual error messages in English.
    – fra-san
    Nov 25 at 14:06










  • I hope you don't have a user called "denied" :-)
    – Kusalananda
    Nov 25 at 14:15






  • 1




    The : not an identifier lines are the key. You have a carriage return character at the end of one or more of your read lines. Get rid of them in an editor. They might be displayed as ^M.
    – Mark Plotnick
    Nov 25 at 21:11






  • 1




    Looks like you definitely have CR (r) in there. Remove that semicolon, then run dos2unix < two.sh > twofixed.sh and see if that new script still has those problems.
    – Mark Plotnick
    Nov 25 at 23:46
















  • 3




    shellcheck.net is a good resource for syntax checking shell scripts. You don't have any glaring errors here: you could improve your quoting and command substitution (as pointed out by shellcheck.net). The main problem with the output is that logbien and logmal contain the grep output which is newline-separated lines. If you want the number of good logins, you need to count those lines: n_bien=$(echo "$logbien" | wc -l)
    – glenn jackman
    Nov 25 at 13:58










  • You may also want to run your script as (export LC_MESSAGES=C; ./your_script) to report the actual error messages in English.
    – fra-san
    Nov 25 at 14:06










  • I hope you don't have a user called "denied" :-)
    – Kusalananda
    Nov 25 at 14:15






  • 1




    The : not an identifier lines are the key. You have a carriage return character at the end of one or more of your read lines. Get rid of them in an editor. They might be displayed as ^M.
    – Mark Plotnick
    Nov 25 at 21:11






  • 1




    Looks like you definitely have CR (r) in there. Remove that semicolon, then run dos2unix < two.sh > twofixed.sh and see if that new script still has those problems.
    – Mark Plotnick
    Nov 25 at 23:46










3




3




shellcheck.net is a good resource for syntax checking shell scripts. You don't have any glaring errors here: you could improve your quoting and command substitution (as pointed out by shellcheck.net). The main problem with the output is that logbien and logmal contain the grep output which is newline-separated lines. If you want the number of good logins, you need to count those lines: n_bien=$(echo "$logbien" | wc -l)
– glenn jackman
Nov 25 at 13:58




shellcheck.net is a good resource for syntax checking shell scripts. You don't have any glaring errors here: you could improve your quoting and command substitution (as pointed out by shellcheck.net). The main problem with the output is that logbien and logmal contain the grep output which is newline-separated lines. If you want the number of good logins, you need to count those lines: n_bien=$(echo "$logbien" | wc -l)
– glenn jackman
Nov 25 at 13:58












You may also want to run your script as (export LC_MESSAGES=C; ./your_script) to report the actual error messages in English.
– fra-san
Nov 25 at 14:06




You may also want to run your script as (export LC_MESSAGES=C; ./your_script) to report the actual error messages in English.
– fra-san
Nov 25 at 14:06












I hope you don't have a user called "denied" :-)
– Kusalananda
Nov 25 at 14:15




I hope you don't have a user called "denied" :-)
– Kusalananda
Nov 25 at 14:15




1




1




The : not an identifier lines are the key. You have a carriage return character at the end of one or more of your read lines. Get rid of them in an editor. They might be displayed as ^M.
– Mark Plotnick
Nov 25 at 21:11




The : not an identifier lines are the key. You have a carriage return character at the end of one or more of your read lines. Get rid of them in an editor. They might be displayed as ^M.
– Mark Plotnick
Nov 25 at 21:11




1




1




Looks like you definitely have CR (r) in there. Remove that semicolon, then run dos2unix < two.sh > twofixed.sh and see if that new script still has those problems.
– Mark Plotnick
Nov 25 at 23:46






Looks like you definitely have CR (r) in there. Remove that semicolon, then run dos2unix < two.sh > twofixed.sh and see if that new script still has those problems.
– Mark Plotnick
Nov 25 at 23:46












1 Answer
1






active

oldest

votes

















up vote
1
down vote













This is not an answer, but a bit of code review: I would offer this to improve your date entry: get the user to enter the date at once, and use the date command to validate and normalize the user's input.



read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d"); then
echo "Error: invalid date" >&2
exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]; then echo "invalid year" >&2; exit 1; fi

# then: grep "$date" logfile ...





share|improve this answer























  • i appreciate that revision! it looks much more cleaner than my initial solution, no doubt. But i still get a lot of error messages. I used the shellcheck and it found no errors whatsoever. I'll add the new code in the post. Thanks!
    – jfalava
    Nov 25 at 16:47











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});


}
});






jfalava is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484040%2frhel-syntax-errors-with-bash-script%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
1
down vote













This is not an answer, but a bit of code review: I would offer this to improve your date entry: get the user to enter the date at once, and use the date command to validate and normalize the user's input.



read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d"); then
echo "Error: invalid date" >&2
exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]; then echo "invalid year" >&2; exit 1; fi

# then: grep "$date" logfile ...





share|improve this answer























  • i appreciate that revision! it looks much more cleaner than my initial solution, no doubt. But i still get a lot of error messages. I used the shellcheck and it found no errors whatsoever. I'll add the new code in the post. Thanks!
    – jfalava
    Nov 25 at 16:47















up vote
1
down vote













This is not an answer, but a bit of code review: I would offer this to improve your date entry: get the user to enter the date at once, and use the date command to validate and normalize the user's input.



read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d"); then
echo "Error: invalid date" >&2
exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]; then echo "invalid year" >&2; exit 1; fi

# then: grep "$date" logfile ...





share|improve this answer























  • i appreciate that revision! it looks much more cleaner than my initial solution, no doubt. But i still get a lot of error messages. I used the shellcheck and it found no errors whatsoever. I'll add the new code in the post. Thanks!
    – jfalava
    Nov 25 at 16:47













up vote
1
down vote










up vote
1
down vote









This is not an answer, but a bit of code review: I would offer this to improve your date entry: get the user to enter the date at once, and use the date command to validate and normalize the user's input.



read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d"); then
echo "Error: invalid date" >&2
exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]; then echo "invalid year" >&2; exit 1; fi

# then: grep "$date" logfile ...





share|improve this answer














This is not an answer, but a bit of code review: I would offer this to improve your date entry: get the user to enter the date at once, and use the date command to validate and normalize the user's input.



read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d"); then
echo "Error: invalid date" >&2
exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]; then echo "invalid year" >&2; exit 1; fi

# then: grep "$date" logfile ...






share|improve this answer














share|improve this answer



share|improve this answer








answered Nov 25 at 14:04


























community wiki





glenn jackman













  • i appreciate that revision! it looks much more cleaner than my initial solution, no doubt. But i still get a lot of error messages. I used the shellcheck and it found no errors whatsoever. I'll add the new code in the post. Thanks!
    – jfalava
    Nov 25 at 16:47


















  • i appreciate that revision! it looks much more cleaner than my initial solution, no doubt. But i still get a lot of error messages. I used the shellcheck and it found no errors whatsoever. I'll add the new code in the post. Thanks!
    – jfalava
    Nov 25 at 16:47
















i appreciate that revision! it looks much more cleaner than my initial solution, no doubt. But i still get a lot of error messages. I used the shellcheck and it found no errors whatsoever. I'll add the new code in the post. Thanks!
– jfalava
Nov 25 at 16:47




i appreciate that revision! it looks much more cleaner than my initial solution, no doubt. But i still get a lot of error messages. I used the shellcheck and it found no errors whatsoever. I'll add the new code in the post. Thanks!
– jfalava
Nov 25 at 16:47










jfalava is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















jfalava is a new contributor. Be nice, and check out our Code of Conduct.













jfalava is a new contributor. Be nice, and check out our Code of Conduct.












jfalava is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484040%2frhel-syntax-errors-with-bash-script%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







Popular posts from this blog

サソリ

広島県道265号伴広島線

Accessing regular linux commands in Huawei's Dopra Linux