You are not logged in.
example from some random config file
i don't want to start a religion; i guess people's eyes are different.
for me the first works better.
Last edited by ohnonot (2021-07-02 08:10:56)
Offline
...no, I'm really not sure which I prefer...
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Offline
I much prefer camelCase to the awkward and ugly underscore (especially when it is the leading character)
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
Offline
Great! Now we can have another flame war!!
Move over emacs<>vim...
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Offline
I much prefer camelCase to the awkward and ugly underscore (especially when it is the leading character)
yeehaaah!
let the flame wars commence!
Offline
damo wrote:I much prefer camelCase to the awkward and ugly underscore (especially when it is the leading character)
yeehaaah!
let the flame wars commence!
http://emojis.slackmojis.com/emojis/ima … 1477057420
_flameWar
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
Offline
Linebreak in string
bash has "dollared single quotes" (see QUOTING in man bash) which let you use backslash escaped sequences. The only one I make regular use of is \n for a newline. So you don't always need to spread a string over multi lines.
Simple strings:
string='first line
second line'
can just have linebreaks put in as-is, but if you're joining variables, separated by linebreaks, it can make for a neater line sometimes:
string="$var1"$'\n'"$var2"
# as opposed to
string="$var1
$var2"
It depends on the situation, and your taste...
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Offline
Great! Now we can have another flame war!!
Move over emacs<>vim...
To add a 3rd:
I don't use either, but lowercase all, example found: $halfwidth
I do use camelCase for fileNames someTimes.
unoconv -f text --stdout file.docx
Last edited by brontosaurusrex (2017-05-17 08:57:18)
Offline
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~
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
Offline
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
Offline
@damo: aaah, i understand now. it looks really clunky but could come in handy in scripting.
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 )
Offline
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 )
Offline
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 )
Offline
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 )
Offline
^ Good point, thanks! I've changed the post.
“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.
Offline