18 July 2014

###字符编码 Q: bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8): No such file or directory bash: warning: setlocale: LC_COLLATE: cannot change locale (en_US.UTF-8): No such file or directory bash: warning: setlocale: LC_MESSAGES: cannot change locale (en_US.UTF-8): No such file or directory bash: warning: setlocale: LC_NUMERIC: cannot change locale (en_US.UTF-8): No such file or directory bash: warning: setlocale: LC_TIME: cannot change locale (en_US.UTF-8): No such file or directory

A: CentOS6

locale -a  查看已经安装的编码

localedef -v -c -i en_US -f UTF-8 en_US.UTF-8  --超级用户权限

###gedit乱码处理

Windows下的 txt 用的是 GBK 编码,而 gedit 默认用的是 UTF-8 编码,所以会出现乱码。在11.10之前,可以通过 gconf-editor 来修改 gedit 的编码方式。在Ubuntu11.10上,改为 dconf-editor。所以先安装 gconf-editor。

最直接简单的方法: Ctrl+o 在打开文件的对话框左下角有字符编码,里面有添加。添加相关的编码即可。比如GB18030,GB2312等等。

如果不行,可以试试下面:

  1. 打开终端,输入sudo apt-get install dconf-tools;
  2. 安装完成后,在终端输入 dconf-editor 并回车;
  3. 这时会打开一个界面,在界面的左边依次选择org——gnome——gedit——preferences——encodings;
  4. 选到encodings时,在右边双击auto-detected,添加’GB18030’,’GB2312’,’GBK’。输完后回车即可。

建议安装软件用终端: sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock

1、查看是否图形键面下的更新在开启 2、切换到root 3、sudo dpkg –configure -a

###文档查看器乱码

(注:我unbuntu12.04没有这样的问题。但是下载的文件名是乱码,下面方法对此无效) 使用Evince打开部分pdf文件,特别是中国知网上下载的pdf论文时,常常显示为方块。 evince,okular, epdfview的pdf功能均由xpdf的分支poppler提供。poppler官方网站:http://poppler.freedesktop.org

必须先安装poppler-data

sudo apt-get install poppler-data

如果已经安装了文泉驿正黑字体,使用下面的方法解决显示方块问题:

sudo gedit  /etc/fonts/conf.d/49-sansserif.conf

将倒数第四行的sans-serif修改为WenQuanYi Zen Hei就可以了。

例如以下,将倒数第四行的字体改为汉字的字体,我改成了文泉驿正黑。

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<!--
If the font still has no generic name, add sans-serif
-->
    <match target="pattern">
        <test qual="all" name="family" compare="not_eq">
            <string>sans-serif</string>
        </test>
        <test qual="all" name="family" compare="not_eq">
            <string>serif</string>
        </test>
        <test qual="all" name="family" compare="not_eq">
            <string>monospace</string>
        </test>
        <edit name="family" mode="append_last">
            <string>文泉驿正黑</string>
        </edit>
    </match>
</fontconfig>

修改为其他的中文字体名称应该也是可以的。 参考:http://www.linuxsky.org/doc/desktop/200801/222.html

一般文档都是用的宋体。如果安装了宋体的话,推荐改为宋体,这样就会看起来比较正常。

###文件编码

如果你需要在Linux 中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题。Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8。下面介绍一下,在Linux中如何查看文件的编码及如何进行对文件进行编码转换。

####查看文件编码 在Linux中查看文件编码可以通过以下几种方式:

1 在Vim 中可以直接查看文件编码

:set fileencoding

即可显示文件编码格式。

如果你只是想查看其它编码格式的文件或者想解决用Vim查看文件乱码的问题,那么你可以在 ~/.vimrc 文件中添加以下内容:

set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936

这样,就可以让vim自动识别文件编码(可以自动识别UTF-8或者GBK编码的文件),其实就是依照fileencodings提供的编码列表尝试,如果没有找到合适的编码,就用latin-1(ASCII)编码打开。

####enca 查看文件编码

如果你的系统中没有安装这个命令,可以用

sudo yum install -y enca 

安装

$ enca filename
filename: Universal transformation format 8 bits; UTF-8 CRLF line terminators 需要说明一点的是,enca对某些GBK编码的文件识别的不是很好,识别时会出现: Unrecognized encoding

###文件编码转换

  • 在Vim中直接进行转换文件编码,比如将一个文件转换成utf-8格式

    :set fileencoding=utf-8

  • iconv 转换,iconv的命令格式如下:

    iconv -f [encoding] -t [encoding] [inputfile]

比如将一个UTF-8 编码的文件转换成GBK编码

iconv -f GBK -t UTF-8 file1 -o file2
  • enconv 转换文件编码,比如要将一个GBK编码的文件转换成UTF-8编码,操作如下

    enconv -L zh_CN -x UTF-8 filename

Q: execute shell script output error :

./network-throughput.sh: 16: ./network-throughput.sh: Syntax error: "(" unexpect")"

A:

The script does not begin with a shebang line, so the kernel executes it with /bin/sh. On Ubuntu, /bin/sh is dash, a shell designed for fast startup and execution with only standard features. When dash reaches line 3, it sees a syntax error: that parenthesis doesn’t mean anything to it in context.

Since dash (like all other shells) is an interpreter, it won’t complain until the execution reaches the problematic line. So even if the script successfully started at some point in your testing, it would have aborted once line 68 was reached.

The shebang line must be the very first thing in the file. Since you use bash features, the first line of the file must be #!/bin/bash or #!/usr/bin/env bash.

Q: sudo wireshark &

error: XDG_RUNTIME_DIR not set in the environment.

(nautilus:9341): Gtk-WARNING **: cannot open display:

A:When you run software as another user you’re in fact starting the new minimal and isolated environment that doesn’t carry on some “excessive” variables (among others variables responsible for injecting libraries or setting certain privileges).

pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY wireshark &

it will carry on user-specific x server settings from the current session:

Q: 使用了这个命令:sudo chmod -R 777 /usr

sudo:must be setuid root 解决方法
sudo: must be setuid root  or
sudo: 有效用户 ID 不是 0,sudo 属于 root 并设置了 setuid 位吗?

A:

  • 进入recovery模式,以root身份登录。
  • 将文件系统重新挂载为rw,否则执行后续命令是可能出现Read-only file system错误, 重新挂载的命令为 mount -o remount rw / 。

  • 恢复相关文件权限。

      chmod 4755 /usr/bin/sudo
      chmod  755 /usr/lib/sudo/sesh
      chmod  644 /usr/lib/sudo/sudoers.so
      chmod  644 /usr/lib/sudo/sudo_noexec.so
    

Q: 搜狗拼音出现异常,挂了 或 导致 CPU 100%

A: 杀死所有 fcitx 进程, sougoupanel, ps -aux | grep 后不能看到任何与 fcitx 和 sogou 相关的进程

fcitx -d > /dev/null 2>&1; sogou-qimpanel > /dev/null 2>&1

ps 查看确保 fctix 和 sogoupanel 都起来了,如果没有起来,继续运行上述对应的命令 $ps aux | grep fcitx

14047  0.5  1.6 350580 65264 ?        Sl   10:44   0:00 fcitx -d
10816  0.2  0.0  39804  1836 ?        Ss   11:11   0:00 //bin/dbus-daemon --fork --print-pid 5 --print-address 7 --config-file /usr/share/fcitx/dbus/daemon.conf
10820  0.0  0.0  13028   604 ?        SN   11:11   0:00 /usr/bin/fcitx-dbus-watcher unix:abstract=/tmp/dbus-C3bUMSNowb,guid=2610fd2be50a09d33de32eb554f7c969 10816

$ps aux | grep sogou

14174  1.9  1.1 3058260 43552 ?       Sl   10:44   0:00 sogou-qimpanel
10865  0.0  0.2  642900 11332 ?        S    11:11   0:00 sogou-qimpanel-watchdog

Q: vim 出现 <200b> 字符 A: sed -i ‘s/\xe2\x80\x8b//g’ inputfile 或 tr -d $(/usr/bin/printf “\u200b”) < inputfile

$ /usr/bin/printf 'X\u200bY\u200bZ' | hexdump -C
00000000  58 e2 80 8b 59 e2 80 8b  5a                       |X...Y...Z|
$ /usr/bin/printf 'X\u200bY\u200bZ' | sed 's/\xe2\x80\x8b//g' | hexdump -C
00000000  58 59 5a        

Q: Pip install: ImportError: cannot import name IncompleteRead A: 按照如下执行 sudo apt-get remove python-pip sudo apt-get autoremove

wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
--no-check-certificate
sudo python get-pip.py

Q: ssh-add -L 执行出错 : Could not open a connection to your authentication agent A: ssh-agent bash

Q: pssh 执行后台命令 A: screen -dmS test /bin/bash -c “cmd” 或 tmux; nohup “cmd” & 存在问题

Q:查看内核版本命令 A: $uname -a $cat /proc/version $lsb_release -a $cat /etc/issue

REDHAT

$cat /etc/redhat-release
$rpm -q redhat-release

Q: 报 shell 脚本最后一行 syntax error: unexpected end of file A: 检查 if 是否存在 fi.

Q: 安装 CentOS 6.7 出现:

Unable to read package metadata. This may be due to missing repodata directory. Please ensure that your install tree has been correctly generated. 
Failare:
repodata/743fec56b2af0ce8d6ec82c47a4efafc2a4d18cddfa9683f29611cb18d1a33de-primary.sqlite.bz2 from anaconda-CentOS-201311271240.i386: [Ermo 256] No more mirrors to try.  

A: 运行该命令 for i in cat TRANS.TBL | awk '{ print $3 }' | grep -v repomd.xml; do mv echo $i | awk -F"-" '{ print $1 }' $i -v; done

Q: CentOS 下如果配置 git 命名补全

A: sudo cp contrib/completion/git-completion.bash /etc/bash_completion.d/ source /etc/bash_completion.d/git-completion.bash sudo yum install bash-completion

Q: 如何查看 rpm 内容

A: rpm2cpio php-5.1.4-1.esp1.x86_64.rpm | cpio -idmv -i = extract -d = make directories -m = preserve modification time -v = verbose