You are not logged in.

#51 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

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

ohnonot
...again
Registered: 2015-09-29
Posts: 3,194
Website

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

#53 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

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

damo
....moderator....
Registered: 2015-08-20
Posts: 4,378

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

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

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

Sector11
Tpyo Knig
From: 77345 ¡#
Registered: 2015-08-20
Posts: 5,150

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!


BunsenLabs Forum Rules ---== I'm a Conky 1.9'er ==---
System:    Host: s12 Kernel: 3.16.0-4-amd64 x86_64 (64 bit gcc: 4.8.4)
Desktop: Openbox 3.5.2 dm: (startx) Distro: Debian GNU/Linux 8

Offline

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

ohnonot
...again
Registered: 2015-09-29
Posts: 3,194
Website

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

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

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 4,673
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


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

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

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 4,673
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.


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

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

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 4,673
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)


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

#60 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

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

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 4,673
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?


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

#62 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

#63 2017-11-13 18:33:35

tknomanzr
BL Die Hard
From: Around the Bend
Registered: 2015-09-29
Posts: 1,029

Re: Handy command-line stuff for terminals or scripts

^ 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

#64 2017-11-14 02:51:39

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 4,673
Website

Re: Handy command-line stuff for terminals or scripts

OT
btw... using bash as login shell in your chroot  tongue
/OT


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

#65 2017-11-14 06:49:03

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

Re: Handy command-line stuff for terminals or scripts

johnraff wrote:

using bash as login shell in your chroot

Yes, it is the default shell for most systems.

Unfortunately tongue


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

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

Offline

#66 2018-01-23 07:19:40

ohnonot
...again
Registered: 2015-09-29
Posts: 3,194
Website

Re: Handy command-line stuff for terminals or scripts

https://github.com/holman/spark/wiki/Wicked-Cool-Usage
smile

PS:
why is this thread not a sticky?

Offline

#67 2018-01-23 07:27:02

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

Re: Handy command-line stuff for terminals or scripts

ohnonot wrote:

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" wink


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

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

Offline

#68 2018-03-11 00:12:56

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

Re: Handy command-line stuff for terminals or scripts

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.

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

Offline

#69 2018-03-11 06:31:34

ohnonot
...again
Registered: 2015-09-29
Posts: 3,194
Website

Re: Handy command-line stuff for terminals or scripts

Head_on_a_Stick wrote:

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)

Offline

#70 2018-03-11 14:19: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

^ Thanks!

ohnonot wrote:

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.

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

Offline

#71 2018-03-11 17:55:01

ohnonot
...again
Registered: 2015-09-29
Posts: 3,194
Website

Re: Handy command-line stuff for terminals or scripts

^ indeed, thanks.
so that is not an extended regex yet?

Offline

#72 2018-03-11 19:37:40

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

Re: Handy command-line stuff for terminals or scripts

^ Erm, *incoherent mumbling about regex*, or something, I think.


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

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

Offline

#73 2018-03-12 06:32:21

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 4,673
Website

Re: Handy command-line stuff for terminals or scripts

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


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

#74 2018-03-12 06:53:45

ohnonot
...again
Registered: 2015-09-29
Posts: 3,194
Website

Re: Handy command-line stuff for terminals or scripts

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

Offline

#75 2018-03-12 07:59:21

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,593

Re: Handy command-line stuff for terminals or scripts

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

Board footer

Powered by FluxBB