Single Line Bash Script Split Hack [Snippet]
This basic script will help to split up a shell script that's been compressed to one line. Come take a look.
Join the DZone community and get the full member experience.Join For Free
This post lists a simple sed script that will split a bash script written in a single line.
Note: this script is a hack; it can produce some wrong output. I've posted it because I've found it helpful in splitting apart bash scripts that have been squashed into a single line. Its also written simply so you can change and extend it easily.
# re-indent bash # call with sed -f sedscript <old >new # http://www.grymoire.com/Unix/Sed.html#uh-16 s/ function /\n\nfunction /g s/ fi /\nfi\n\n/g s/ if \[ /\n\nif [ /g s/; then /; then\n/g s/ else / \nelse /g s/ \([[:alnum:]_]\+\)=/\n\n\1=/g s/export/\nexport/g s/# /\n# /g s/ for/\nfor/g s/ while /\nwhile /g s/ read /\nread /g s/ trap /\ntrap /g s/ sed /\nsed /g s/ rm /\nrm /g s/ chmod /\nchmod /g
Copy this and save it as sedscript. Call with
sed -f sedscript multiline.sh
HTML Escape / Unescape at [link]
Published at DZone with permission of Zach Pfeffer, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.