Archive for the ‘Scripting’ Category
Posted by jpluimers on 2020/06/25
From a while back, but still relevant as the speed of the GMail web-UI still has not improved.
[WayBack/Archive.is] Peeking under the hood of redesigned Gmail – Boris – Medium
Via:
–jeroen
Read the rest of this entry »
Posted in CSS, Development, GMail, Google, HTML, JavaScript/ECMAScript, Power User, Scripting, Software Development, Web Development | Leave a Comment »
Posted by jpluimers on 2020/06/17
Cool: you can return strings both as a function result, and by reference: they are explained in the question, second and fourth answer of [WayBack] How to return a string value from a Bash function – Stack Overflow.
Returning them by reference has two important benefits:
- it is much faster (especially useful in tight loop)
- you can use
echo (normally used to return a result) for debugging purposes
I also needed a bit of switch magic which I found at [WayBack] bash – Switch case with fallthrough? – Stack Overflow and array magic (from [WayBack] Array variables) as arrays are far more readable than indirection (on the why not, see [WayBack] BashFAQ/006 – Greg’s Wiki: How can I use variable variables (indirect variables, pointers, references) or associative arrays?).
So here is a function that returns a specific IPv4 octet.
function getIpv4Octet() {
IPv4=$1
octetIndex=$2
outputVariable=$3
slice="${IPv4}"
count=1
while [ "${count}" -le 4 ]
do
octet[${count}]="${slice%%.*}"
slice="${slice#*.}"
count=$((count+1))
done
case "${octetIndex}" in
"1" | "2" | "3" | "4")
;;
*)
octetIndex="4"
;;
esac
eval $outputVariable="${octet[$octetIndex]}"
}
You call it like this:
$ getIpv4Octet "192.168.178.32" 3 result && echo ${result}
178
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2020/06/16
I needed to enumerate all the parameters to a function and access many of them by index in the same function, so thanks to both these:
I got at this:
declare -a arguments=("$@")
for index in ${!arguments[@]}; do
echo $index/${#arguments[@]}:${arguments[$index]}
done
These are all forms of Array handling or Shell Parameter Expansion with special cases for array variables:
! does indirection, in this case from the array to the index of the array
# gets the lengt of the parameter (for arrays: the number of elements)
[] acccesse an array variable using an index
Via:
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2020/06/10
A while ago, I had to execute a series of aliases of which the names were stored in an array. A simple for loop with en eval call did the job.
Then I found out there are at least two more ways of evaluation in bash, so here are just a few links giving me a head start if I ever dig this up again:
Note that looping over parameters is different than over an array: [WayBack] Loop through an array of strings in Bash? – Stack Overflow
ou can use it like this:
## declare an array variable
declare -a arr=("element1" "element2" "element3")
## now loop through the above array
for i in "${arr[@]}"
do
echo "$i"
# or do whatever with individual element of the array
done
# You can access them using echo "${arr[0]}", "${arr[1]}" also
Also works for multi-line array declaration
declare -a arr=("element1"
"element2" "element3"
"element4"
)
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2020/06/09
I’ve added this to my ~/.bashrc to stuff installed by pip install --user is accessible from interactive shells:
# set PATH so it includes user's private python "pip --user" bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
PATH="$PATH:$HOME/.local/bin"
fi
The addition is at the end of the path. It is a choice: it means machine installs take prevalence over user installs. That’s usually what I want. For more considerations (including non-interactive shells), see [WayBack] bash – How to correctly add a path to PATH? – Unix & Linux Stack Exchange.
The --user installs do not affect the full system, nor other users.
Further reading:
–jeroen
Posted in Development, Python, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2020/06/09
This is part of a bash alias where I had to use quadruple backslash in order to escape it for sed:
# The sed with quad //// is to prevent 'unterminated substitute in regular expression':
alias x='... | sed "s/=.*/ \\\\/"'
This is needed because bash will escape \\\\ into \\ which sed then escapes to \.
The easiest way to find this is to replace the sed with echo to see the expansion.
References:
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, sed, Software Development | 2 Comments »
Posted by jpluimers on 2020/06/03
I read [WayBack] Cursor Movement earlier than [WayBack] Colours and Cursor Movement With tput and [WayBack] The Floating Clock Prompt.
So in one of my scripts I’ve now used an escape sequence, but I might change it to tput in a future version:
## Move one line up, then write finished scripts:
echo -e "\033[1A$finished\r"
I would probably have started with put if I had read [WayBack] bash – Set or change vertical position of the cursor – Stack Overflow first.
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2020/06/02
I hardly do this, so I tend to forget that unset -f functionname deletes a function and unset variablename or unset -v variablename deletes a variable.
From:
I have done this:bash $ z() { echo ‘hello world’; }How do I get rid of it?
Source: [WayBack] How do I delete a Bash function? – Stack Overflow
Reference: [WayBack] unset Man Page – Bash – SS64.com
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2020/06/01
Cool: [WayBack] GitHub – kzahel/web-server-chrome: An HTTP Web Server for Chrome (chrome.sockets API)
This allows you to develop HTTP applications that live in Chrome:
[Archive.is1/Archive.is2] Web Server for Chrome – Chrome Web Store: A Web Server for Chrome, serves web pages from a local folder over the network, using HTTP. Runs offline.
Of course you could to python -m SimpleHTTPServer 8888 or python -m http.server 8888, but this runs within chrome and can be used from inside JavaScript projects.
Features
- serve local files
- configure listening port
- configure listening interface (e.g. localhost or all interfaces)
- custom http handlers possible
- websocket support available
- works nice with chrome.runtime.onSuspend
- options for autostart, start in background, etc etc.
- handles range requests, HEAD, etc
- options for CORS
- optional PUT, DELETE request (for upload files)
- sets MIME types
- can render directory listing
- See relevant options: https://github.com/kzahel/web-server-chrome/blob/master/polymer-ui/options.js
Via [WayBack] This is super useful: A# web #server that runs in #Chrome! Makes it super easy to do local web dev without the hassle of setting up a complex back end s… – Jason Mayes – Google+
–jeroen
Posted in Chrome, Development, Google, JavaScript/ECMAScript, Power User, Scripting, Software Development, Web Development | Leave a Comment »