Your bash++ post inspired me to whip up a fun little one liner. I say, with enough semicolons, any bash script can be a one line bash script! May I present, a bash one line script that will turn...
Your bash++ post inspired me to whip up a fun little one liner.
If you are writing a script that is more than 100 lines long, or that uses non-straightforward control flow logic, you should rewrite it in a more structured language now.
I say, with enough semicolons, any bash script can be a one line bash script! May I present, a bash one line script that will turn any other bash script into a one line script:
while IFS= read -r line; do while [[ "${line:0:1}" =~ ($'\t'| ) ]]; do line="${line:1}"; done; if [[ "${line:0:1}" == "#" || -z "${line}" ]]; then continue; fi; if [[ "$line" =~ [^\\#][[:space:]]+# ]]; then line="${line% #*}"; fi; if [[ -n "${line}" ]]; then [[ "$line" =~ \b(do|then)$ ]] && output+="$line " || output+="$line; "; fi; done < "${1}"; output="${output//;;/;}"; echo "${output#;}"
Wrote it in standard markdown to make sure it worked, then manually combined it to a single line because I have a smooth brain and didn't think to use it on itself. The readable/original: while...
Wrote it in standard markdown to make sure it worked, then manually combined it to a single line because I have a smooth brain and didn't think to use it on itself.
The readable/original:
while IFS= read -r line; do
# Strip leading tabs and white space
while [[ "${line:0:1}" =~ ($'\t'| ) ]]; do
line="${line:1}"
done
# If it's a comment or blank line, skip it
if [[ "${line:0:1}" == "#" || -z "${line}" ]]; then
continue
fi
# If it has a trailing comment, remove the trailing comment
if [[ "$line" =~ [^\\#][[:space:]]+# ]]; then
line="${line% #*}"
fi
# If we still do not have a blank line, combine the output
if [[ -n "${line}" ]]; then
[[ "$line" =~ \b(do|then)$ ]] && output+="$line " || output+="$line; "
fi
done < "${1}"
# Remove redundant semicolons
output="${output//;;/;}"
# Trim any accidental leading semicolons
echo "${output#;}
Unfortunately upon reread I'm noting that my comment completely lacks the joke-y tone it should've. I do use heredocs on occasion, usually for stuff like embedding a small config file where I only...
Unfortunately upon reread I'm noting that my comment completely lacks the joke-y tone it should've. I do use heredocs on occasion, usually for stuff like embedding a small config file where I only have a single entry point such as putting a systemd service config into an EC2 userdata script, which just made me wonder what funny behaviors it may have if fed one. I was expecting it to fail, but what I wasn't expecting and was a bit amused by was that the EOF string at the end of the heredoc mysteriously vanished.
I'll fuckin do it again
Your bash++ post inspired me to whip up a fun little one liner.
I say, with enough semicolons, any bash script can be a one line bash script! May I present, a bash one line script that will turn any other bash script into a one line script:
Did you write it in 1 line originally, or in multiple lines and then run it on its own code to make it single line?
Wrote it in standard markdown to make sure it worked, then manually combined it to a single line because I have a smooth brain and didn't think to use it on itself.
The readable/original:
Sadly this isn't ready for production. Heredocs cause an invalid output file.
Never much cared for or used heredoc's myself, so didn't account for them. I guess I should say, turn most other bash scripts into a one line script.
Unfortunately upon reread I'm noting that my comment completely lacks the joke-y tone it should've. I do use heredocs on occasion, usually for stuff like embedding a small config file where I only have a single entry point such as putting a systemd service config into an EC2 userdata script, which just made me wonder what funny behaviors it may have if fed one. I was expecting it to fail, but what I wasn't expecting and was a bit amused by was that the EOF string at the end of the heredoc mysteriously vanished.
Ah, but have you considered matrix text underneath a water effect shader in space in your terminal while you write your OpenGL functions?
This is a good bit, by all means never stop
Disgusting, thanks for posting!
Where do you keep finding these?!
It’s all interesting, and (unfortunately) still about Bash all the same. 😭
Ew.
If this were IRC, this is where you would
/nick hungarianbash
Keep up the good work.