Category: Unix

Env

Posted by – February 15, 2012

#!/usr/bin/env bash
Probably the most common use of env is to find the correct interpreter
for a script, when the interpreter may be in different directories on
different systems.  The following example will find the `perl’ inter-
preter by searching through the directories specified by PATH.
1.  #!/usr/bin/env perl
One limitation of that example is that it assumes the user’s value for
PATH is set to a value which will find the interpreter you want to exe-
cute.  The -P option can be used to make sure a specific list of directo-
ries is used in the search for utility.  Note that the -S option is also
required for this example to work correctly.
2. #!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl
The above finds `perl’ only if it is in /usr/local/bin or /usr/bin.  That
could be combined with the present value of PATH, to provide more flexi-
bility.  Note that spaces are not required between the -S and -P options:
3. #!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl

#!/usr/bin/env bash
Probably the most common use of env is to find the correct interpreter     for a script, when the interpreter may be in different directories on     different systems.  The following example will find the `perl’ inter-     preter by searching through the directories specified by PATH.
1.  #!/usr/bin/env perl     One limitation of that example is that it assumes the user’s value for     PATH is set to a value which will find the interpreter you want to exe-     cute.  The -P option can be used to make sure a specific list of directo-     ries is used in the search for utility.  Note that the -S option is also     required for this example to work correctly.
2. #!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl
The above finds `perl’ only if it is in /usr/local/bin or /usr/bin.  That     could be combined with the present value of PATH, to provide more flexi-     bility.  Note that spaces are not required between the -S and -P options:
3. #!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl

Using tar together with gzip

Posted by – September 22, 2011

How can I extract a tar.gz or .tgz file?
Files with extension tar.gz or .tgz are tar files compressed with gzip. On Unix extract them with:

gunzip < file.tar.gz | tar xvf –
gunzip < file.tgz    | tar xvf –
Can gzip compress several files into a single archive?
Not directly. You can first create a tar file then compress it:

tar cvf –  filenames | gzip > file.tar.gz

stty 显示和修改终端行设置

Posted by – September 13, 2011

学习和转载与: http://codingstandards.iteye.com/blog/826924

用途说明

stty命令用于显示和修改终端行设置(change and print terminal line settings)。

常用参数

stty命令不带参数可以打印终端行设置,加上-a参数可以打印得更详细些。

stty size可以显示终端的大小,即行数和列数。

stty命令还可以更改终端行的设置,格式如下:

stty SETTING CHAR

其中,SETTING可以是如下

eof : 输入结束,文件结束,默认为Ctrl+D。比如:用cat >file来创建文件时,按Ctrl+D来结束输入。
erase : 向后删除字符,擦除最后一个输入字符,默认为Ctrl+?。注意默认情况下退格键Backspace不是删除字符。
intr : 中断当前程序,默认为Ctrl+C。
kill : 删除整条命令,删除整行,默认为Ctrl+U。
quit :退出当前程序,默认为Ctrl+\或Ctrl+|。
start : 启动屏幕输出,默认为Ctrl+Q。
stop :停止屏幕输出,默认为Ctrl+S。有时候终端突然僵死了,可能是不小心按了Ctrl+S的缘故,因为我们习惯性的按Ctrl+S来保存文件。
susp : terminal stop当前程序,默认为Ctrl+Z。这样当前进程就会变成后台进程了。

werase:删除最后一个单词,默认为Ctrl+W。

stty命令还有一些其他用法,如:

stty -echo 关闭回显。比如在脚本中用于输入密码时。

stty echo 打开回显。

输入密码的脚本片段:stty -echo; read var; stty echo; 或 read -s var

使用示例

示例一 打印终端行设置

[root@web ~]# stty
speed 38400 baud; line = 0;
-brkint -imaxbel
[root@web ~]# stty -a
speed 38400 baud; rows 41; columns 132; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;
susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -cdtrdsr
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

[root@web ~]#

示例二 打印当前终端的大小(行数和列数)

[root@web ~]# stty size
41 132

示例三 设置退格键Backspace的删除行为

在默认情况下,我们按退格键Backspace时,会在屏幕上回显^H,而不是把前一个字符删除。比如使用sftp/ftp/sqlplus/ij等命令时,就会碰到这种情况。我们可以使用stty命令把Backspace的行为变成删除前一个字符。

[root@web ~]# sftp 192.168.6.12
Connecting to 192.168.6.12…
root@192.168.6.12’s password:
sftp> get abc^H^H^H^H
Couldn’t stat remote file: No such file or directory
File “/root” not found.
sftp> quit
[root@web ~]#
[root@web ~]# stty erase ^H
[root@web ~]# sftp 192.168.6.12
Connecting to 192.168.6.12…
root@192.168.6.12’s password:
sftp> get abc
Couldn’t stat remote file: No such file or directory
File “/root/abc” not found.
sftp> quit
[root@web ~]#

示例四 在vi编辑文件时按Ctrl+Q来结束终端僵死的局面

[root@web ~]# vi 1.txt
1
2
3
Ctrl+S
~

注:按了Ctrl+S之后,就会禁止屏幕输出,从而出现终端僵死的情况。

这个时候,只要按Ctrl+Q就会结束这种局面,因为它会允许屏幕输出。

Ctrl+Q

示例五 在bash脚本中与tty命令配合使用

在下面的脚本中,先取得终端文件名称,以判断脚本是否运行在交互式方式还是在管道方式。

Bash代码  收藏代码
  1. #!/bin/sh
  2. CONSOLE_INPUT=$(tty)
  3. echo “CONSOLE_INPUT=$CONSOLE_INPUT”
  4. COMMAND_LINE=”java -Xmx640m -classpath .:lib/hyjc.jar:lib/log4j-1.2.15.jar:lib/commons-logging-1.1.1.jar:lib/proxool-0.9.0RC3.jar:lib/mysql-connector-java-5.1.10-bin.jar:lib/poi-3.5-ZHY-20091107.jar:lib/jdom-1.1.jar hyjc.sql.DBCLI MhrDB.properties MhrDB”
  5. if [ “$CONSOLE_INPUT” == “not a tty” ]; then
  6. $COMMAND_LINE
  7. elif [ “$(whereis rlwrap)” == “rlwrap:” ]; then
  8. stty erase ^H
  9. $COMMAND_LINE
  10. else
  11. rlwrap -f jdb.rlwrap $COMMAND_LINE
  12. fi

Bash Colors

Posted by – June 20, 2011

Bash Color Escape Codes

Echo (echo -e) the following escape codes inside \e[ESCCODEm to colorize text in Bash:

  • Black 0;30
  • Dark Gray 1;30
  • Blue 0;34
  • Light Blue 1;34
  • Green 0;32
  • Light Green 1;32
  • Cyan 0;36
  • Light Cyan 1;36
  • Red 0;31
  • Light Red 1;31
  • Purple 0;35
  • Light Purple 1;35
  • Brown 0;33
  • Yellow 1;33
  • Light Gray 0;37
  • White 1;37

Make sure to use echo -e to enable interpretation of backslash escapes:

bash$ echo -e "This is red->\e[00;31mRED\e[00m"

Remove Color

Echo \e[00m to remove text color modifications:

bash$ echo -n '\e[00m'

mapping key in vim

Posted by – November 2, 2010

Refers to:
http://man.chinaunix.net/newsoft/vi/doc/map.html

Adding your wanna mapping key into /etc/vim/vimrc to enable map function in vim.

Ubuntu下的图形化分区管理工具–gparted

Posted by – October 10, 2010

Linux下也有类似PQ Magic一样的图形化分区管理工具,他的名字叫gparted:
[bash]
sudo apt-get install gparted
[/bash]

sudo without enter password

Posted by – October 10, 2010

1、使用root权限运行以下命令:
[bash]
visudo
[/bash]

2、把/etc/sudoers(自动打开)裡面最後一行:
[bash]
%admin ALL=(ALL)ALL
[/bash]

改為:
[bash]
%admin ALL=(ALL) NOPASSWD: NOPASSWD: ALL
[/bash]

kde desktop applet info

Posted by – October 7, 2010

1. kwriteconfig – Write KConfig entries (for use in shell scripts):
http://manpages.ubuntu.com/manpages/gutsy/man1/kwriteconfig.1.html

2.  kreadconfig – Read KConfig entries (for use in shell scripts)
http://manpages.ubuntu.com/manpages/gutsy/man1/kreadconfig.1.html

3.  kde desktop applet src file locaiton:
This file recorder .desktop file location, groups info, etc…
We can change desktop files position by mouse on KDE Desktop, below file will record it…
the program kdeinit4: plasma-desktop [kdeinit]  recorde the poistion to file below.

/home/<userName>/.kde/share/config/plasma-desktop-appletsrc: ( below is a entry of firefox.desktop in this file)
[Containments][14][Applets][33]
geometry=46,391.745912850909,78.7183612323325,87.2540871490914                  (x,y)=46,391.745912850909, icon size=(78.7183612323325, 87.2540871490914)-(x,y)
immutability=1
plugin=icon
zvalue=116

[Containments][14][Applets][33][Configuration]
Url=file:///usr/share/cocreate-default-settings/firefox.desktop

4. /usr/bin/startkde auto start when new user login Kde X window.

5. plasma-desktop

install ubuntu 10.10 on hard disk using iso file

Posted by – September 13, 2010

To install Ubuntu on hard disk using iso file:

1. Download ubuntu iso file to a disk partition(e.g.: /dev/sda3) root(/) dir which NOT the partition will install Ubuntu.

[bash]
cd /
wget -c http://cdimage.ubuntu.com/daily-live/current/maverick-desktop-i386.iso
[/bash]

2. Only extract vmlinuz and initrd.lz from iso file the casper dir to the root(/) dir the iso file downloaded.
3. Reboot the box and enter into grub:

[bash]
reboot
[/bash]

Once rebooted, when see the grub list, enter ‘c’ to enter into grub.
4. Type below grub commands to install Ubuntu from hard disk:

[bash]
root (hd0,3)
kernel /vmlinuz   boot=casper    iso-scan/filename=/maverick-desktop-i386.iso ro quiet splash locale=zh_CN.UTF-8
initd    /initrd.lz
boot
[/bash]

4. Once enter into the new system, installing ubuntu by clicking the icon on your desktop, and follow the tips to finish installation.

Note:
1. You can renmae iso file for short.
2. You can remove the locale parameter from the kernel( if not work, use ‘linux‘ instead) command.

Enjoy!

Ubuntu mount permanently

Posted by – September 9, 2010

Ubuntu mount permanently: ( if not root, use sudo before each command below)
1. mkfs.ext3 or mkfs.ext4 your device like below:

[code lang=”bash”]mkfs.ext3 /dev/sda4
[/code]

2. Modify /etc/fstab, and append below line to the file:

[bash]/dev/sda4    /home/freespace    ext3    default,noatime    0     0
[/bash]

3. reboot your box:

[bash]reboot[/bash]

Tip:
Using more /etc/fstab to see more info about how to add entry to fstab file see below:

[bash]
root@JinLab:~/sda4# more /etc/fstab
[/bash]

# /etc/fstab: static file system information.
#
# Use ‘blkid -o value -s UUID’ to print the universally unique identifier
# for a device; this may be used with UUID= as a more robust way to name
# devices that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
# / was on /dev/sda2 during installation
UUID=c1bc5ff4-157b-4e33-a30f-12e5691d77d0 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda1 during installation
UUID=69230e89-79fa-4bca-a37f-ce9461c31129 none            swap    sw              0       0

# /root/sda4 on /dev/sda4
UUID=2dbba927-0fb3-4ad0-97f0-9887364e5edc /root/sda4       ext3 defaults,noatime 0    0