By example I will show you Bash One Time Temporary Functions (I’m not sure if they existed before me, if not, I just invented them) or simply Temp Functions. Take that circle with C and R in it. ®ock and ©opy®ight 

Format of the One Time Temp Functions:

function FUNCTIONNAME { dostuff; unset FUNCTIONNAME; }; FUNCTIONNAME yourvariable1

For convenience you would only copy this part: function FUNCTIONNAME { dostuff; unset FUNCTIONNAME; }; FUNCTIONNAME So that when you paste it you can you can just start typing the yourvariable1  part and hit enter to get the whole thing you see above

NOTE: you can use more than 1 variable. You just need to space seperate them.

function FUNCTIONNAME { dostuff; unset FUNCTIONNAME; }; FUNCTIONNAME yourvariable1 yourvariable2

 

Example1:

Here is a SQLITE3 one that. Prints every table with line mode (instead of default output mode). And one that prints every table with headers on (using default output mode). The benefit is that the File Variable is at the end, so when I copy paste it. I can immediately supply the file. The function which was created, only runs once because its deleted immediately by the unset call at the end of the function. This works awesome for copy pasteable command.

function sql3printl { for t in $(sqlite3 "${1}" .tables); do echo "--- ${1} -- ${t} ---"; ECHOCMD=".mode line\nselect * from \"${t}\";"; echo -e "$ECHOCMD" | sqlite3 "${1}"; done; unset sql3printl; }; sql3printl FILENAME

NOTE: you would just copy everything aside from FILENAME, then when you use the function over and over you will paste it and just type your filename and it will run everything in sql3printl against your file.

Example2:

Same general idea except instead of printing with line mode. We print regularly with headers.

function sql3printh { for t in $(sqlite3 "${1}" .tables); do echo "--- ${1} -- ${t} ---"; ECHOCMD=".headers on\nselect * from \"${t}\";"; echo -e "$ECHOCMD" | sqlite3 "${1}"; done; unset sql3printh; }; sql3printh FILENAME

Why its awesome

To look at why its awesome. Lets look at the slow old way and compare.

Here is the way to do it without a function. The issue is that I have to go to the beginning of the line to edit the File Variable. Which doesnt play well with copy pasteable commands.

## example 1 - sqlite3 line mode print all tables - none temp function (the regular long method) ###

F="PUTFILENAMEHERE"; for t in $(sqlite3 "${F}" .tables); do echo "--- ${F} -- ${t} ---"; ECHOCMD=".mode line\nselect * from \"${t}\";"; echo -e "$ECHOCMD" | sqlite3 "${F}"; done;


## example 2 - sqlite3 print all tables with headers - none temp function (the regular long method) ###

F="PUTFILENAMEHERE"; for t in $(sqlite3 "${F}" .tables); do echo "--- ${F} -- ${t} ---"; ECHOCMD=".headers on\nselect * from \"${t}\";"; echo -e "$ECHOCMD" | sqlite3 "${F}"; done;

NOTE: this sucks for copy pasting because you have to paste it. Then you have to go to the front of the line and edit inside the parenthesis (which is more keystrokes to get inthere). Then you have to probably go to the end of the line to just hit ENTER (or maybe not, but either way it sucks and it takes too many milliseconds). Then after that you have a loose annoying F variable.

If you wanted to get rid of the loose annoying F varible you can put the whole thing in parenthesis so that its in a subshell. Thus making the F variable only temporarily exist while the subshell exists. This just adds more navigational keystrokes.

## example 1 - sqlite3 line mode print all tables - none temp function (the regular long method) ###

(F="PUTFILENAMEHERE"; for t in $(sqlite3 "${F}" .tables); do echo "--- ${F} -- ${t} ---"; ECHOCMD=".mode line\nselect * from \"${t}\";"; echo -e "$ECHOCMD" | sqlite3 "${F}"; done;)


## example 2 - sqlite3 print all tables with headers - none temp function (the regular long method) ###

(F="PUTFILENAMEHERE"; for t in $(sqlite3 "${F}" .tables); do echo "--- ${F} -- ${t} ---"; ECHOCMD=".headers on\nselect * from \"${t}\";"; echo -e "$ECHOCMD" | sqlite3 "${F}"; done;)

NOTE: another reason the old method sucks and the temp function method is better is if you have multiple variables to edit. You will need to add a whole lot more keystrokes. But with my temp function method your variables are just one space bar press away, saving many keystrokes.


The End! Send all the cred & gold. Thank you very much!

Leave a Reply

Your email address will not be published. Required fields are marked *