You are not logged in.

#21 2015-12-12 13:01:50

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

Re: Handy command-line stuff for terminals or scripts

Freeing up some memory

Running this before and after will show results:

free -m

At the user level:

sync && echo 3 > sudo tee /proc/sys/vm/drop_caches

At the root level (ie: sudo su - first) just a slight change

sync && echo 3 > /proc/sys/vm/drop_caches

Root level in action:

 12 Dec 15 | 09:50:59 ~
    $ sudo su
[sudo] password for sector11: 
root@bunsen:/home/sector11# free -m
             total       used       free     shared    buffers     cached
Mem:          5985       1285       4699         23        101        633
-/+ buffers/cache:        550       5435
Swap:         2047          0       2047
root@bunsen:/home/sector11# sync && echo 3 > /proc/sys/vm/drop_caches
root@bunsen:/home/sector11# free -m
             total       used       free     shared    buffers     cached
Mem:          5985        714       5271         24          2        194
-/+ buffers/cache:        517       5468
Swap:         2047          0       2047
root@bunsen:/home/sector11# 

Just freed up 33MB.  wink

Edit - typing 'exit' while in the root terminal of course brings you back out to user level - or just close the terminal.

Last edited by Sector11 (2015-12-12 13:04:22)


The sun will never set if you keep walking towards it. - my son
Being positive doesn't understand physics.
_______________________________
Debian 10 Buster

Offline

#22 2015-12-13 01:18:55

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

Re: Handy command-line stuff for terminals or scripts

^...or press Ctrl+d to exit root.
Press it at user level and the terminal will close.


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

#23 2015-12-13 05:41:28

gako
Member
Registered: 2015-10-02
Posts: 241

Re: Handy command-line stuff for terminals or scripts

if there is text in terminal ctrl+d will delete that first

Hey while we're at it...
Not a snippet of code but still handy command line stuff.

Search:
ctrl+n/p = up/down in command history
ctrl+r = reverse search while typing. keep tapping ctrl+r to search
ctrl+s = forward search / brick terminal lol hmm
ctrl+q = unbrick terminal smile

Navigate Line:
ctrl+a/e = move cursor to start or end of line
ctrl+b/f = same as arrow keys, move one at a time
ctrl+left_arrow/right_arrow = move one word at a time
alt+b/f = same as above
press esc, then either b or f = same as above

Delete:
ctrl+d = delete text char under cursor. if no text exit terminal
ctrl+k = delete text from cursor to end of line
ctrl+x and then press Backspace = delete text from cursor to begin of line

Transpose:
ctrl+t = swaps characters before and under cursor
press esc and then t = swaps whole words before cursor
esc then u = change uppercase forward one word
esc then l = change lowercase forward one word
esc then c = change uppercase letter under cursor

Editor:
ctrl+x followed by ctrl+e = launch default editor.

Offline

#24 2016-01-04 10:35:09

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Handy command-line stuff for terminals or scripts

Testing if a variable is set or empty in bash:

Use the functions 'is_set' and 'is_empty' from this script:

#!/bin/bash

is_set() { eval "[[ -n \"\${$1+.}\" ]]"; }
# for a variable to be empty two conditions need to be true
#     1. the variable is set
#     2. the variable has the emty string as value
is_empty(){ is_set $1 && ! eval "[[ -n \"\${$1:+.}\" ]]"; }
report() {
  [ "$#" -gt 0 ] && printf >&2 '%s\n' "$@"
}
printf "case: testvar is unset\n"
is_set testvar && report "testvar is set" || report "testvar is not set"
is_empty testvar && report "testvar is empty" || report "testvar is not empty"

printf "case: testvar=\n"
testvar=

is_set testvar && report "testvar is set" || report "testvar is not set"
is_empty testvar && report "testvar is empty" || report "testvar is not empty"

printf "case: testvar1=''\n"
testvar1=''

is_set testvar1 && report "testvar1 is set" || report "testvar1 is not set"
is_empty testvar1 && report "testvar1 is empty" || report "testvar1 is not empty"

printf "case: testvar2='x'\n"
testvar2='x'

is_set testvar2 && report "testvar2 is set" || report "testvar2 is not set"
is_empty testvar2 && report "testvar2 is empty" || report "testvar2 is not empty"

Offline

#25 2016-01-09 03:28:12

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

Re: Handy command-line stuff for terminals or scripts

^I understand the use of eval is perfectly safe here because it's arguments are quite restricted, but having been trained to avoid it as something evil, I remembered declare -p as another way of getting a variable's state:

is_set() { declare -p $1 >/dev/null 2>&1;}
is_empty(){ is_set $1 && ! [[ ${!1} ]];}

This seems to do the same while being slightly easier to read - for me anyway...

However, it depends on 'declare -p' returning failure if given an unset variable. I presume that can be relied upon?

Last edited by johnraff (2016-02-16 06:26:41)


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

#26 2016-01-09 12:16:48

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Handy command-line stuff for terminals or scripts

^ Agreed.

This comes up every so often. Searching for recent solutions, I found out that since bash 4.2 there is a command to test if a variable is set.

is_set() { [[ -v $1 ]];}
is_empty(){ is_set $1 && ! [[ ${!1} ]];}

works.

Offline

#27 2016-01-10 00:34:44

KrunchTime
Member
Registered: 2015-09-29
Posts: 857

Re: Handy command-line stuff for terminals or scripts

This tip comes from Matthew Garrett via the Jan 2016 issue of Linux Voice (page 16):

Forgot to type sudo before a command?  sudo!! will re-run the previous command under sudo.  But not just sudo; it can be used to precede the previous command with whatever comes before the !!.

Offline

#28 2016-01-10 00:43:41

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

Re: Handy command-line stuff for terminals or scripts

In a similar vein:

Warning: this link contains profanity

That program is in the official Arch repositories and is actually tremendously useful.

I suppose you could always alias it to something else if the swearing bothered you.


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

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

Offline

#29 2016-01-10 02:10:48

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

Re: Handy command-line stuff for terminals or scripts

KrunchTime wrote:

This tip comes from Matthew Garrett via the Jan 2016 issue of Linux Voice (page 16):

Forgot to type sudo before a command?  sudo!! will re-run the previous command under sudo.  But not just sudo; it can be used to precede the previous command with whatever comes before the !!.


Oh man, that is good... but knowing me, I'd forget to add more than one exclamation mark or something, making it easier just to retype the command with sudo in front tongue   But yea this is a good one!


"I have not failed, I have found 10,000 ways that will not work" -Edison

Offline

#30 2016-01-13 19:44:25

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

Re: Handy command-line stuff for terminals or scripts

Hey guys, has anyone used the tool 'ethtool'  command?
ethtool man page

As someone who does a lot of work on servers running linux that need to be plugged into switches and using a lot of virtual VM's at work this cool command is a great way to query the layer 2 interface stats. Check out some of the layer of detail you can get:

# ethtool eth0
Settings for eth0:
        Supported ports: [ TP ]
        Supported link modes:   10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
                                1000baseT/Full
        Supports auto-negotiation: Yes
        Advertised link modes:  10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
                                1000baseT/Full
        Advertised auto-negotiation: Yes
        Speed: 100Mb/s
        Duplex: Full
        Port: Twisted Pair
        PHYAD: 1
        Transceiver: internal
        Auto-negotiation: on
        Supports Wake-on: d
        Wake-on: d
        Link detected: yes

And you can issue commands and changes to the interfaces!

# ethtool -s eth0 speed 100 autoneg off

# ethtool eth0
Settings for eth0:
        Supported ports: [ TP ]
        Supported link modes:   10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
                                1000baseT/Full
        Supports auto-negotiation: Yes
        Advertised link modes:  Not reported
        Advertised auto-negotiation: No
        Speed: Unknown! (65535)
        Duplex: Unknown! (255)
        Port: Twisted Pair
        PHYAD: 1
        Transceiver: internal
        Auto-negotiation: off

Maybe it's just me, but I think this is pretty awesome!


"I have not failed, I have found 10,000 ways that will not work" -Edison

Offline

#31 2016-01-25 04:46:43

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

Re: Handy command-line stuff for terminals or scripts

Well this is something that's probably well known to many of you, but a nifty safety guard for those still trying to get into the habit of remembering to "append" rather than "overwrite"

kingghidorah@bunsen:~$ set +o noclobber

kingghidorah@bunsen:~$ echo some_text_here > test.txt


kingghidorah@bunsen:~$ echo some_new_text > test.txt

bash: test.txt: cannot overwrite existing file

kingghidorah@bunsen:~$ echo some_good_text >> test.txt


kingghidorah@bunsen:~$


kingghidorah@bunsen:~$ cat test.txt
some_text_here
some_good_text

So basically keep the noclobber variable set and it'll force you to get into the habit of adding the double >> to append!


"I have not failed, I have found 10,000 ways that will not work" -Edison

Offline

#32 2016-08-01 08:00:27

becker_11
Member
From: Perth, Western Australia
Registered: 2015-12-30
Posts: 66

Re: Handy command-line stuff for terminals or scripts

This returns the size (in human readable format) of the current directories contents excluding any subdirectories.

ls -lh | head -1

Offline

#33 2016-08-01 08:03:14

becker_11
Member
From: Perth, Western Australia
Registered: 2015-12-30
Posts: 66

Re: Handy command-line stuff for terminals or scripts

Horizon_Brave wrote:
KrunchTime wrote:

This tip comes from Matthew Garrett via the Jan 2016 issue of Linux Voice (page 16):

Forgot to type sudo before a command?  sudo!! will re-run the previous command under sudo.  But not just sudo; it can be used to precede the previous command with whatever comes before the !!.


Oh man, that is good... but knowing me, I'd forget to add more than one exclamation mark or something, making it easier just to retype the command with sudo in front tongue   But yea this is a good one!

To add sudo to the previous command press "up arrow" then "Ctrl+a" to move the cursor to the beginning of the line. Then add "sudo" & press enter.

Offline

#34 2016-08-01 15:45:52

Nili
Member
From: $HOME/♫♪
Registered: 2015-09-30
Posts: 1,045
Website

Re: Handy command-line stuff for terminals or scripts

I like the "calendar" command

nili ~ $ calendar
Aug 01 	Herman Melville born, 1819
Aug 01 	Lughnasa; Feast of the god Lugh, a 30 day Celtic feast centers on this day
Aug 01 	Discovery Day in Trinidad and Tobago
Aug 01 	Emancipation Day in Granada
Aug 01 	Founding of Asuncion in Paraguay
Aug 01 	Freedom Day in Guyana
Aug 01 	National Day in Switzerland
Aug 01 	National Holidays (5 days) in El Salvador
Aug 01 	Parent's Day in Zaire
Aug 01 	The Concert for Bangla Desh takes place at Madison Square Garden, 1971
Aug 01 	Lugnasad / Lughnasada / Lunasa - Gaelic summer "games of Lug" (sun-god)
Aug 01*	Summer bank holiday (Scotland)
Aug 02 	Our Lady of Los Angeles in Costa Rica
Aug 02*	Parshat Tetzaveh
Aug 02 	Lady Godiva Day
Aug 02 	Gabor Kovesdan <gabor@FreeBSD.org> born in Budapest, Hungary, 1987

Fedora GNOME / Devuan XFCE
Roots heritage #!

Offline

#35 2017-05-12 07:01:49

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

Re: Handy command-line stuff for terminals or scripts

You'll often read that it's not a good idea to use upper-case variable names in scripts, because they might clash with one of the environment variables, which are all upper-case. I was reminded of this yesterday, after spending almost a day trying to debug a script which was trying to use an ssh connection. It failed to put up a passphrase entry box, so errored out for lack of authentication, even though the same command was working OK when run by itself.

It turned out I was using 'TERM=x-terminal-emulator' at one point. Of course everyone knows (right?) that TERM is an important environment variable (try 'echo $TERM') and by overwriting it I was breaking ssh's pseudo-tty function so I couldn't enter the passphrase.

So... DON'T USE UPPER-CASE VARIABLE NAMES.


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

#36 2017-05-12 07:03:11

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

Re: Handy command-line stuff for terminals or scripts

^ also: never force $TERM, it breaks things.

Always let the terminal emulator set that variable.

Last edited by Head_on_a_Stick (2017-05-12 07:03:36)


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

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

Offline

#37 2017-05-12 18:45:19

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

Re: Handy command-line stuff for terminals or scripts

Need a script checker to check for variable collisions for not just internal variables but checks the env. variables too!


"I have not failed, I have found 10,000 ways that will not work" -Edison

Offline

#38 2017-05-13 03:24:17

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

Re: Handy command-line stuff for terminals or scripts

^Just avoid upper case and you'll be OK. All envvars are UC.


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

#39 2017-05-13 12:07:17

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

Re: Handy command-line stuff for terminals or scripts

^ and for readability, i found that the under_score really helps to make variables stand out visually. more so than camelCase, which i hate, hate, hate with a passion.
and never make the string 'var' part of your variable! that's like naming your domain something-online.com.

Offline

#40 2017-05-14 03:42:13

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

Re: Handy command-line stuff for terminals or scripts

ohnonot wrote:

^ and for readability, i found that the under_score really helps to make variables stand out visually. more so than camelCase, which i hate, hate, hate with a passion.

hmm so variable_name vs variableName...
Still not sure which I prefer. The underscore does require one more keystroke, but it is a bit easier on the eyes. I used to find camelCase really ugly, but it doesn't bother me quite so much these days.


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

Board footer

Powered by FluxBB