You are not logged in.

#41 2017-05-14 10:43:15

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: Handy command-line stuff for terminals or scripts

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

#42 2017-05-15 05:04:48

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,971
Website

Re: Handy command-line stuff for terminals or scripts

...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 )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#43 2017-05-15 21:00:27

damo
....moderator....
Registered: 2015-08-20
Posts: 6,734

Re: Handy command-line stuff for terminals or scripts

I much prefer camelCase to the awkward and ugly underscore (especially when it is the leading character) hmm


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

#44 2017-05-16 01:10:31

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,971
Website

Re: Handy command-line stuff for terminals or scripts

Great! Now we can have another flame war!!
Move over emacs<>vim... cool


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#45 2017-05-16 06:24:10

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: Handy command-line stuff for terminals or scripts

damo wrote:

I much prefer camelCase to the awkward and ugly underscore (especially when it is the leading character) hmm

yeehaaah!
let the flame wars commence!
trolldance.gif?1477057420

Offline

#46 2017-05-16 07:43:20

damo
....moderator....
Registered: 2015-08-20
Posts: 6,734

Re: Handy command-line stuff for terminals or scripts

ohnonot wrote:
damo wrote:

I much prefer camelCase to the awkward and ugly underscore (especially when it is the leading character) hmm

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

#47 2017-05-17 02:55:19

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,971
Website

Re: Handy command-line stuff for terminals or scripts

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 )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#48 2017-05-17 08:51:04

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,464
Website

Re: Handy command-line stuff for terminals or scripts

johnraff wrote:

Great! Now we can have another flame war!!
Move over emacs<>vim... cool

To add a 3rd:
I don't use either, but lowercase all, example found: $halfwidth
I do use camelCase for fileNames someTimes.

Handling docx

unoconv -f text --stdout file.docx

Last edited by brontosaurusrex (2017-05-17 08:57:18)

Offline

#49 2017-05-17 17:38:34

geekosupremo
Member
Registered: 2016-04-27
Posts: 168

Re: Handy command-line stuff for terminals or scripts

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. devil

Although Vim is good for generating random data when given to a user the very first time. So many random keystrokes.  devil

Offline

#50 2017-05-18 05:44:21

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: Handy command-line stuff for terminals or scripts

johnraff wrote:
string="$var1"$'\n'"$var2"

isn't that the same as

string="$var1\n$var2"

???

geekosupremo wrote:

Vim is good for generating random data when given to a user the very first time. So many random keystrokes.

only towards the end:

exasperated noob wrote:

qqqqqQQQQqqqQQqqqqQQ^Cqqqqfuhgq2uiåeghq24~

Offline

#51 2017-05-18 17:19:52

geekosupremo
Member
Registered: 2016-04-27
Posts: 168

Re: Handy command-line stuff for terminals or scripts

ohnonot wrote:

only towards the end:

exasperated noob wrote:

qqqqqQQQQqqqQQqqqqQQ^Cqqqqfuhgq2uiåeghq24~

Fair, but with a little scripting I'm sure it could be worked around. wink

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 devil

Offline

#52 2017-05-18 19:51:59

damo
....moderator....
Registered: 2015-08-20
Posts: 6,734

Re: Handy command-line stuff for terminals or scripts

ohnonot wrote:
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

#53 2017-05-18 20:00:37

Sector11
Conky 1.9er Mod Squid
From: Upstairs
Registered: 2015-08-20
Posts: 6,945

Re: Handy command-line stuff for terminals or scripts

geekosupremo wrote:

Although natually I'd prefer nano or emacs devil

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

#54 2017-05-19 06:40:37

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: Handy command-line stuff for terminals or scripts

@damo: aaah, i understand now. it looks really clunky but could come in handy in scripting.

Offline

#55 2017-05-22 01:19:42

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,971
Website

Re: Handy command-line stuff for terminals or scripts

ohnonot wrote:

...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

cool


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#56 2017-09-16 08:22:38

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,971
Website

Re: Handy command-line stuff for terminals or scripts

indirect variable

Having spent over a day on a meaningless task, I ran into this. smile
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 )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#57 2017-10-30 09:36:57

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,971
Website

Re: Handy command-line stuff for terminals or scripts

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. smile

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 )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#58 2017-11-12 12:23:58

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 8,759
Website

Re: Handy command-line stuff for terminals or scripts

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.

Forum Rules   •   How to report a problem   •   Software that rocks

Offline

#59 2017-11-13 03:04:38

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,971
Website

Re: Handy command-line stuff for terminals or scripts

^Very handy. smile

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 )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#60 2017-11-13 06:50:23

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 8,759
Website

Re: Handy command-line stuff for terminals or scripts

^ Good point, thanks! I've changed the post.


“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.

Forum Rules   •   How to report a problem   •   Software that rocks

Offline

Board footer

Powered by FluxBB