You are not logged in.
I generally prefer the under_score method myself. Makes it clear that there is a word separation.
camelCase is fine ... just less readable at a glance.
And emacs is better obviously.
Although Vim is good for generating random data when given to a user the very first time. So many random keystrokes.
Offline
string="$var1"$'\n'"$var2"
isn't that the same as
string="$var1\n$var2"
???
Vim is good for generating random data when given to a user the very first time. So many random keystrokes.
only towards the end:
qqqqqQQQQqqqQQqqqqQQ^Cqqqqfuhgq2uiåeghq24~
Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos
Offline
only towards the end:
exasperated noob wrote:qqqqqQQQQqqqQQqqqqQQ^Cqqqqfuhgq2uiåeghq24~
Fair, but with a little scripting I'm sure it could be worked around.
If you have an iOS device, and you only sometimes use Vim, it might be useful to have Vimmy. It's a nice little reference app. No actual text editing, but I've saved myself a ton of anger with it.
Although natually I'd prefer nano or emacs
Offline
johnraff wrote:string="$var1"$'\n'"$var2"
isn't that the same as
string="$var1\n$var2"
???
Doesn't look like it...
damo@graphix ~ $ string="$var1"$'\n'"$var2"
damo@graphix ~ $ echo $string
Some text some more text
damo@graphix ~ $ echo "$string"
Some text
some more text
damo@graphix ~ $ string="$var1\n$var2"
damo@graphix ~ $ echo $string
Some text\nsome more text
damo@graphix ~ $ echo "$string"
Some text\nsome more text
Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt «» BunsenLabs on DeviantArt
Online
Although natually I'd prefer nano or emacs
And for Android users there's:
Vim Quick Reference - and the DroidVim (Text Editor)
Linux Emacs Editor Manual and couldn't fine an emacs editor and forget nano!
The sun will never set if you keep walking towards it. - my son
Being positive doesn't understand physics.
_______________________________
Debian 10 Buster = SharpBang ♯!
Offline
@damo: aaah, i understand now. it looks really clunky but could come in handy in scripting.
Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos
Offline
...it looks really clunky but could come in handy in scripting.
That's it, really. Occasionally it's useful to be able to put a linebreak in somewhere with a $'\n'
john@bunsen1:~$ v='one line'
john@bunsen1:~$ echo "${v/ /$'\n'}"
one
line
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Online
indirect variable
Having spent over a day on a meaningless task, I ran into this.
While associative arrays are often the best way to go with indirection, sometimes it's handy to have a variable point to another variable by name, especially in functions, for example, so you can give a function the name of a variable and have things done with it.
TL:DR Use "declare" to indirectly set a variable: 'declare -g $varname=$varcontents'
See:
john@bunsen1:~$ var='some string'
john@bunsen1:~$ name=var
john@bunsen1:~$ echo "$name"
var
john@bunsen1:~$ echo "${!name}"
some string
john@bunsen1:~$ $name='new string' # doesn't work
bash: var=new string: command not found
john@bunsen1:~$ declare $name='new string'
john@bunsen1:~$ echo "${!name}"
new string
BTW you need the -g (global) option with declare inside a function if you want to use the variable outside the function later - declare makes local variables by default.
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Online
Sort an array
Google as usual, and as is often the case stackoverflow comes up with the goods.
None of the answers there did exactly what I wanted, though I liked the bubble_sort option for some reason. (Might not be efficient with big arrays?) Anyway, unlike solutions piping stuff to sort etc., this allows the array members to hold any strange characters, including newlines.
Making its array local, but referenced (man bash > nameref) to whatever array name you passed it, lets you use this function as-is in any script: copy/paste or source a file.
Then I grabbed the idea of a generic comparison function from a different answer, made the option, er, optional, to get this:
#!/bin/bash
# example comparison functions:
compare_sort_ascii() { test "$1" \> "$2" ; } # ascii order
compare_sort_locale() { [[ "$1" > "$2" ]] ; } # lexicographical order (default)
compare_mtime() { [[ $1 -ot $2 ]]; } # useless unless $1 & $2 are real files!
# adapted from https://stackoverflow.com/a/7446365/4964251
# Usage: bubble_sort [-c|--cmpfun function-name] array-name
# Sorts global array array-name by lexicographical (or other) order.
# Can use bubble_sort -c compare_sort_ascii (etc. etc.) to override default.
bubble_sort() {
case "$1" in
-c|--cmpfun)
[[ $(type -t "$2") = 'function' ]] || { echo "${2}: not a function" >&2; return 1;}
local compare_fun=$2
shift 2
;;
*)
compare_fun() { [[ $1 > $2 ]]; }
;;
esac
local -n BSORT="$1"
local ubound=$((${#BSORT[*]} - 1))
while ((ubound > 0))
do
local i=0
while ((i < ubound))
do
if "$compare_fun" "${BSORT[$i]}" "${BSORT[$((i + 1))]}"
then
local t="${BSORT[$i]}"
BSORT[$i]="${BSORT[$((i + 1))]}"
BSORT[$((i + 1))]="$t"
fi
((++i))
done
((--ubound))
done
}
# Demonstration:
arr=('bar' 'zap' 'ZAP' 'BBC' 'Alpha' 'two
entries' 'Pale
Ale')
echo "unsorted-------------------"
for i in "${arr[@]}"
do
echo "$i"
done
bubble_sort --cmpfun compare_sort_locale arr
echo "sorted (locale lexi)-------"
for i in "${arr[@]}"
do
echo "$i"
done
bubble_sort --cmpfun compare_sort_ascii arr
echo "sorted (ascii)-------------"
for i in "${arr[@]}"
do
echo "$i"
done
EDIT: added the bash shebang, just to make things clear.
Last edited by johnraff (2017-11-13 03:00:32)
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Online
I chroot into systems so much that I've made a function for it in my shell initialisation file:
function mnt {
for i in proc sys dev dev/pts; do sudo mount --bind /$i "$@1"/$i; done &
sudo chroot "$1" /bin/bash
sudo umount -R "$1"/{proc,sys,dev}
}
So if the target filesystem tree is mounted under /mnt (for example) then this will chroot into it with the API filesystems bind mounted so that upgrades and stuff work (and clean up afterwards):
mnt /mnt
Last edited by Head_on_a_Stick (2017-11-13 06:50:40)
“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.
Offline
^Very handy.
But, are you sure "$@" is appropriate? Does this function really handle multiple paths passed simultaneously?
"$1" maybe?
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Online
^ Good point, thanks! I've changed the post.
“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.
Offline
^ I've been using this bit ever since @HoaS posted it the first time. Chroot is indeed a handy tool. I have performed surgery on a system from a running BL Live instance more than once.
Offline
OT
btw... using bash as login shell in your chroot
/OT
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Online
using bash as login shell in your chroot
Yes, it is the default shell for most systems.
Unfortunately
“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.
Offline
https://github.com/holman/spark/wiki/Wicked-Cool-Usage
PS:
why is this thread not a sticky?
Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos
Offline
why is this thread not a sticky?
We try to avoid "sticky overload" as it makes the boards messy.
Just keep bumping the thread with nice tips to keep it "alive"
“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.
Offline
Reverse grep a file to print out the contents with any empty lines, or lines starting with a comment, removed:
grep -v '^$\|^#' $file
“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.
Offline
Reverse grep a file to print out the contents with any empty lines, or lines starting with a comment, removed:
grep -v '^[[:space:]]*$\|^[[:space:]]*#' $file
to also remove lines containing nothing but spaces, or comments starting with spaces.
PS: if i use 'grep -vE' this doesn't work. i never know when to use -E and when not...
Last edited by ohnonot (2018-03-11 06:32:54)
Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos
Offline
^ Thanks!
if i use 'grep -vE' this doesn't work. i never know when to use -E and when not...
The \ character serves as an escape to allow | to work as if -E had been passed.
To make it work with -E, drop the escape character:
grep -vE '^#|^$' $file
“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.
Offline
^ indeed, thanks.
so that is not an extended regex yet?
Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos
Offline
^ Erm, *incoherent mumbling about regex*, or something, I think.
“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.
Offline
I don't think extended regexes are all that special really - it mostly seems to be about whether you need to escape certain characters or not. Unless I'm missing something (not uncommon).
There are things like pcre though...
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Online
^ which grep also has a switch for.
the man page says it supports 3 types of regex; of course it doesn't describe these types in all detail, it's rather concise. all the more reason to read it in its entirety, me.
Please use CODE tags for code.
Search youtube without a browser: repo | thread
BL quote proposals to this thread please.
my repos / my repos
Offline
man pages in firefox (has tabs)
apt install groff
alias bman='BROWSER=firefox-esr man --html' # add to your .bash_aliases
bman vim # example
weather and small weather
alias weather="wget wttr.in 2>/dev/null -O - | grep -v 'New feature*\|Follow'"
alias today='wget wttr.in/?0Q 2>/dev/null -O -'
ascii from image
alias asciize='img2txt -W "$( tput cols )" -f utf8 -d random'
asciize someimage.png # example
Last edited by brontosaurusrex (2018-03-12 10:36:32)
Offline