?

Log in

No account? Create an account

Jun. 3rd, 2009

cocteau

Bash command line tips

1. Brace expansion:

$ echo {a..c}{0..5}
a0 a1 a2 a3 a4 a5 b0 b1 b2 b3 b4 b5 c0 c1 c2 c3 c4 c5

eg. use to remove files with common prefix:

$ rm program.{c,h,o}

2. Base Conversion:

$ echo $((0x1FFFFF))
2097151
$ echo $[0x1fffff]
2097151
$ echo $[16#1fffff]
2097151

$ echo $[2#111111]
63

$ declare -i result
$ result=2#111111
$ echo $result
63

For arbitrary conversions to bases other than decimal use dc and here strings <<<:

$ dc <<< "2o 10i 17 p"
10001
$ dc <<< "2o 16i 10F p"
100001111

(2o = output base 2, 16i = input base 16, p = print)
 
3. Arithmetic:

$ echo $[5*33]
165
$ echo $[2**31]
2147483648
$ echo $[2**52%53]
1

For large numbers and floating point, use bc and here strings <<<:

$ echo $[2**64]
0
$ bc <<< "2^64"
18446744073709551616
$ bc <<< "2.3+5.8"
8.1
$ bc <<< "2.3222+5.8111101"
8.1333101

add -l to specify math functions, eg a()=atan():

$ bc -l <<< "4*a(1)"
3.14159265358979323844
$ bc -l <<< "scale=100;4*a(1)"
3.141592653589793238462643383279502884197169399375105820974944592307\
8164062862089986280348253421170676

4. for loop C-style:

$ for ((i=o; i<10; i++)); do echo -n $i" "; done
0 1 2 3 4 5 6 7 8 9

nth roots of 2, n=1-10, to 20 decimal places:

$ for ((n=1;n<=10;n++)); do bc -l <<< "scale=20;e(l(2)/$n)"; done
1.99999999999999999998
1.41421356237309504878
1.25992104989487316476
1.18920711500272106671
1.14869835499703500679
1.12246204830937298142
1.10408951367381233764
1.09050773266525765919
1.08005973889230616986
1.07177346253629316421

5. while read loop:

<command>   | while read i; do echo "$i"; done

or, if newlines are not the delimiter specify IFS:

IFS=:;<command> | while read i; do echo "$i"; done

6. pseudo random number:

$ echo $RANDOM
29833
$ echo $RANDOM
1123
$ echo $RANDOM
4235

7. string parsing, eg extract filename prefix:

$ str="file.txt"
$ echo ${str%%.*}
file

May. 20th, 2009

cocteau

nfs shares

The standard file sharing protocol on linux is nfs. There are alternatives such as ftp, scp/sshfs and samba but nfs is particularly easy to configure, except for the firewall.

Assume you have a partition/drive you want to share with other computers, and that it is mounted on /media/DATA locally. Then add this to /etc/exports:

/media/DATA         *(rw, sync)

If you only want to share with a subnet then use something like 10.0.0.0/24 instead of *, ensure there is no space before the following bracket which specifies mount options. See 'man exports' for details

Now restart nfs service, and temporarily disable the firewall so we can test:

su -
service nfs restart
service iptables stop

Assume the nfs server has ip 10.0.0.3, then on a remote client mount the share with:

su -
mkdir /mnt/data
mount -t nfs 10.0.0.3:/media/DATA /mnt/data

If all is well you can now configure the firewall on the nfs server. Add these lines to the end of /etc/sysconfig/nfs:

LOCKD_TCPPORT=4001
STATD_PORT=4004
RQUOTAD_PORT=4005
LOCKD_UDPPORT=4002
MOUNTD_PORT=4003

Now you must open ports 4001-4005 (udp/tcp), 2049 and 111 on the firewall, do this using system-config-firewall gui, then check the following output from su -c 'iptables-save'

iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 2049 -j ACCEPT
iptables -A INPUT -p udp -m state --state NEW -m udp --dport 2049 -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 111 -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 4001:4005 -j ACCEPT
iptables -A INPUT -p udp -m state --state NEW -m udp --dport 4001:4005 -j ACCEPT


Ensure iptables is running:

su -c 'service iptables restart'

One final point, the man page for exports incorrectly states that export names can have spaces, this is not the case and you should remount the share locally on a mount point that does not contain spaces.

May. 19th, 2009

cocteau

man

Want a summary of the linux filesystem hierarchy?

man hier

Other useful pages: ascii, bootparam, intro, standards, suffixes, syscalls, units.

(If man pages are missing (eg on LiveCD) type: su -c 'yum install man-pages')

Want to list all man pages with a one line summary?

man -k . | less

(If no output then run /usr/sbin/makewhatis (as root) first)

List user commands:

man -k . | grep "(1)"

List sysadmin commands:

man -k . | grep "(8)"

List manual pages for configuration files:

man -k . | grep "(5)" | grep -i conf

How about changing the formatting of man pages, eg if you don't like the underlined font but want a color instead then you can set some environment variables for the default pager, less:

export LESS_TERMCAP_us=$'\E[01;34m'
export LESS_TERMCAP_ue=$'\E[0m'

the colors are,
Black 00;30 Dark Gray 01;30
Blue 00;34 Light Blue 01;34
Green 00;32 Light Green 01;32
Cyan 00;36 Light Cyan 01;36
Red 00;31 Light Red 01;31
Purple 00;35 Light Purple 01;35
Brown 00;33 Yellow 01;33
Light Gray 00;37 White 01;37

Similarly, you can change the default bold text:

export LESS_TERMCAP_md=$'\E[01;30m'
export LESS_TERMCAP_me=$'\E[0m'

Finally, don't forget info, it often contains more "info" than the corresponding man page. eg 'info date' is much more comprehensive than 'man date', and if you want to find out why gcc won't compile your c++ program type:

info gcc  --index-search="g++"
cocteau

Screen captures

There are several ways to capture the screen in Fedora. Gnome has it's own screen capture utility gnome-screenshot, which you can activate immediately with Print Scrn, or Alt + Print Scrn to just capture the active window. gnome-screenshot can be launched from a terminal, and that allows several useful options such as:

$ gnome-screenshot --help
...
Application Options:
  -w, --window                   Grab a window instead of the entire screen
  -a, --area                     Grab an area of the screen instead of the entire screen
  -b, --include-border           Include the window border with the screenshot
  -B, --remove-border            Remove the window border from the screenshot
  -d, --delay=seconds            Take screenshot after specified delay [in seconds]
  -e, --border-effect=effect     Effect to add to the border (shadow, border or none)
  -i, --interactive              Interactively set options
  --display=DISPLAY              X display to use

(--interactive is particularly useful)

For more advanced screen captures with image processing, resizing etc you can use the import command from the ImageMagick package. As an example, this will capture the screen after a 5 sec delay, resize the image by 60% and save in jpeg format as 'screen-hhmmss_DD/MM/YY.jpg'

su -c 'yum install ImageMagick'

import -pause 5 -silent -window root -resize 60% "screen-$(date +'%H%M%S_%F')".jpg

There are other utilities that will achieve similar results with less overhead, eg try 'yum install scrot', 'man scrot'

Finally, you may want more than a screen capture, suppose you want to make a movie of the whole screen session? Well, there are dedicated screencast apps like istanbul and xvidcap, but my favourite is ffmpeg! (video only)

su -c 'yum install ffmpeg'

ffmpeg -y -f x11grab -r 12 -s 1280x800 -i :0.0 screengrab.avi

That will start recording your widescreen laptop session in mpeg4 video at 12 frames per second, press q to stop. To obtain better quality you can specify -sameq in the options, that should give you the same quality as the original screen, at the expense of more disk space required to store the movie.
cocteau

Introduction

Hi, I'll be blogging about linux, mainly Fedora: http://fedoraproject.org

Linux is now a very mature desktop operating system, and almost anything that you can do in Windows you can do in Linux ( except high quality gaming, but we all use consoles for that now don't we? ;) )

Linux is particularly cool on Netbooks, I'm posting this from Fedora 11 on an Acer Aspire One.

OK, first test post over, back soon.
cocteau

June 2009

S M T W T F S
 123456
78910111213
14151617181920
21222324252627
282930    

Syndicate

RSS Atom
Powered by LiveJournal.com