Article note: *niceadvice*

If you’ve ever been debugging with a fellow developer, you’ll hear “OK, execute that and let me know what it says”.  In this case, you can either manually copy the output and instant message the text over to them, or you can write the output to file with >> , open the file, manually copy the contents, and paste it into IM.  I experience this for a few hours recently and it was way to much work!  Shouldn’t there be a way to quickly place an execution’s output directly into the clipboard just to save some time?  You can with pbcopy!

Copy stdout to Clipboard

You’ll use a single pipe to transfer the stdout result into the clipboard:

# command | pbcopy
hg diff | pbcopy

The git diff information is copied to the clipboard in this example; now you can show your colleague what you’ve changed.

Copy File Contents to Clipboard

In the case of copying file contents into the clipboard, pbcopy goes first:

# pbcopy < file.ext
pbcopy < circle.yml

The complete file contents are instantly copied to the clipboard for easy sharing.

Pasting to File

So what if you want to paste the clipboard contents into a new or existing file?  Use pbpaste:

#pbpaste > file.txt
pbpaste > commands.txt

The clipboard contents will be placed into the given file.

pbcopy will be a big timesaver for me moving forward.  Manually copy and pasting information is with the mouse or trackpad is inconvenient and time-consuming.  These types of command line techniques can make us more proficient, skilled developers!

The post Copy to Clipboard from Command Line appeared first on David Walsh Blog.

Old but not bus­ted … – Die­ser Inhalt wur­de vor mehr als 9 Jah­ren publi­ziert. Die Kor­rekt­heit und Ver­füg­bar­keit von Links kön­nen lei­der nicht gewähr­leis­tet werden.

Ges­tern wollte/musste ich mal wis­sen, was in einem LXC-Con­tai­ner (auf unse­rem Kom­mu­ne-Ser­ver) gemoun­tet ist. – Da das im ers­ten Anlauf nicht ganz so tri­vi­al her­aus zu bekom­men war, hier mal mei­ne klei­ne Gedankenstütze.

long story short

root@host:~/# ls -lha /proc/$(lxc-info -n container -p | awk '{print $2}')/root/home

Erklärbär

Szenario

  • LXC-Ver­si­on: 0.7.5 (ja ich weiß, is alt … ;))
  • Host = hn
  • Con­tai­ner = container
  • Mount = in der fstab (host:/var/lib/lxc/container/fstab) ist das host:/home mit der Zei­le "/home home none bind 0 0" ein­ge­bun­den (d.h. also, dass beim Star­ten des LXC-Con­tai­ners des /home vom Host beim Start des Con­tai­ners in den Con­tai­ner gemoun­tet wird; dabei ist home als rela­ti­ver Pfad zum / angegeben)

Das Problem

Wenn man nun auf dem Host wis­sen will, wie das /home im Con­tai­ner aus­sieht (ls -lha /var/lib/lxc/container/rootfs/home) wird man fest­stel­len, dass es ganz anders aus­sieht, als erwar­tet. – Hin­ter­grund ist, dass der obi­ge Mount in einem tem­po­rä­ren File­sys­tem (also nicht wie ein normaler/echter Mount) ein­ge­hängt wird.

Hier mal die Aus­ga­be, die mit einem Stan­dard-LXC-Tem­p­la­te (Ubun­tu) erzeugt wird:

root@host:~/# ls -lha /var/lib/lxc/container/rootfs/home
total 4.0K
drwxr-xr-x  3 root   root     19 Aug 18  2012 .
drwxr-xr-x 22 root   root   4.0K Feb  7 14:15 ..
drwxr-xr-x  2 ubuntu ubuntu   54 Aug 20  2012 ubuntu

Die Lösung

Nach etwas Recher­che stieß ich auf den Blog-Post „LXC 1.0: Advan­ced con­tai­ner usa­ge“ des LXC-Ent­wick­lers Sté­pha­ne Gra­ber, in dem der Trick (und eini­ge Hin­ter­grün­de) erklärt werden.

Jeder Con­tai­ner hat eine eige­ne Pro­zess­num­mer (pid). Die­se bekommt man mit lxc-info -n container -p heraus.
Der im tem­po­rä­ren Datei­sys­tem ein­ge­häng­te Mount befin­det sich unter host:/proc und dort wie­der unter der jewei­li­gen PID.
Also ange­nom­men unser Con­tai­ner hat die PID 1234, dann fin­det man des­sen root-File­sys­tem (inkl. aller Mounts) unter host:/proc/1234/root/.

Wir benö­ti­gen also zuerst die PID des Con­tai­ners (lxc-info -n container -p) und danach kön­nen wir uns das File­sys­tem anzei­gen las­sen (ls -lha /proc/PID/root/). – Bei­de Befeh­le kann man nun kombinieren.

mit LXC 1.x

In oben genann­ten Blog-Post wird für das Eru­ie­ren der PID lxc-info -n container -p -H (ange­passt!) ange­ge­ben. Dabei ste­hen die Schal­ter -n container für den Con­tai­ner­na­men, -p für die Aus­ga­be der PID und -H (wahr­schein­lich; sie­he unten) für die nume­ri­sche Aus­ga­be der PID. 

Die Kom­bi­na­ti­on der bei­den Befeh­le zum Anzei­gen des Root-Datei­sys­tems für den Con­tai­ner sieht dann so aus: ls -lha /proc/$(lxc-info -n container -p -H)/root/ (ange­passt!).

mit LXC 1.x (in meinem Fall 0.7.5)

Da ich ATM aller­dings noch nicht die Ver­si­on 1.x ver­wen­de, funk­tio­niert der Tipp (aus dem Blog-Post für die Ver­si­on 1.x lei­der nicht (so ganz).
Denn in 0.7.5 gibt es den Schal­ter -H bei der Aus­ga­be der PID mit lxc-info -n container -p nicht, so dass man nicht nur die PID, son­dern den Text pid: 1234 zurück bekommt. :-/

Aller­dings ist das nicht so dra­ma­tisch, denn man kann sich ja mit awk behel­fen (und somit den Schal­ter ersetzen/emulieren). 🙂
lxc-info -n container -p | awk '{print $2}' lie­fert nur die zwei­te ‚Spal­te‘ der lxc-info-Aus­ga­be, also die num­me­ri­sche PID.

Ergo hier nun mei­ne Lösung zum Anzei­gen des Root-Datei­sys­tems des Containers:

root@host:~/# ls -lha /proc/$(lxc-info -n container -p | awk '{print $2}')/root/home
total 4.0K
drwxr-xr-x  3 root   root    19 Aug 18  2012 .
drwxr-xr-x 22 root   root  4.0K Feb  7 14:15 ..
drwxr-xr-x  2 user1  user1       […]         user1
drwxr-xr-x  2 user2  user2       […]         user2
drwxr-xr-x  2 user3  user3       […]         user3

… wie­der was gelernt … 😉 – Aller­dings wohl nicht für lan­ge, denn das Update auf die Ver­si­on 1.x steht ja vor der Tür …