欢迎光临
我们一直在努力

Linux使用过程中的一些技巧

    • Linux shell 中单引号的技巧
    • 一般情况下双引号引用变量。 其实,两组单引号之间也可以直接引用变量。 示例如下:


<span class="hljs-keyword js-evernote-checked" data-evernote-id="550">Var</span>=Hello
<span class="hljs-keyword js-evernote-checked" data-evernote-id="551">echo</span> <span class="hljs-string js-evernote-checked" data-evernote-id="552">''</span>${<span class="hljs-keyword js-evernote-checked" data-evernote-id="553">Var</span>}<span class="hljs-string js-evernote-checked" data-evernote-id="554">' World! '</span>
    • 注意:在某些情况下,变量中不能含有分割符.

    • 利用sed在行首与行尾添加字符

<span class="hljs-attribute js-evernote-checked" data-evernote-id="556">sed</span> <span class="hljs-string js-evernote-checked" data-evernote-id="557">'s/^/HEAD&/g'</span> /path/to/file
sed <span class="hljs-string js-evernote-checked" data-evernote-id="558">'s/$/&TAIL/g'</span> /path/to/file

    • 利用sed在文件尾部添加行

<span class="hljs-attribute js-evernote-checked" data-evernote-id="560">sed</span> <span class="hljs-string js-evernote-checked" data-evernote-id="561">'<span class="hljs-variable js-evernote-checked" data-evernote-id="562">$a</span>\ADDLINE'</span> /path/to/file

    • 利用sed替换大小写
      • 将所有字符替换为小写:
        <span class="hljs-attribute js-evernote-checked" data-evernote-id="567">echo</span> -n <span class="hljs-string js-evernote-checked" data-evernote-id="568">"<span class="hljs-variable js-evernote-checked" data-evernote-id="569">$TEXT</span>"</span>|sed -r <span class="hljs-string js-evernote-checked" data-evernote-id="570">'s/(.*)/\L\1/'</span>
      • 将所有字符替换为大写:
        <span class="hljs-attribute js-evernote-checked" data-evernote-id="573">echo</span> -n <span class="hljs-string js-evernote-checked" data-evernote-id="574">"<span class="hljs-variable js-evernote-checked" data-evernote-id="575">$text</span>"</span>|sed -r <span class="hljs-string js-evernote-checked" data-evernote-id="576">'s/(.*)/\U\1/'</span>

    • wget的POST方法下载

<span class="hljs-attribute js-evernote-checked" data-evernote-id="578">wget</span> --continue --<span class="hljs-literal js-evernote-checked" data-evernote-id="579">no</span>-check-certificate \
--header=<span class="hljs-string js-evernote-checked" data-evernote-id="580">"Cookie: <span class="hljs-variable js-evernote-checked" data-evernote-id="581">$cookie</span>"</span> \
--post-data=<span class="hljs-string js-evernote-checked" data-evernote-id="582">"DATA"</span> \
<span class="hljs-string js-evernote-checked" data-evernote-id="583">"https://domain.name/url"</span> \
-O <span class="hljs-string js-evernote-checked" data-evernote-id="584">"/path/to/File.Name"</span>

    • curl一般使用方法

<span class="hljs-attribute js-evernote-checked" data-evernote-id="586">curl</span> -sS --insecure --create-dirs \
--retry <span class="hljs-number js-evernote-checked" data-evernote-id="587">3</span> --retry-delay <span class="hljs-number js-evernote-checked" data-evernote-id="588">10</span> \
-H <span class="hljs-string js-evernote-checked" data-evernote-id="589">'Host: domain.name'</span> \
-H <span class="hljs-string js-evernote-checked" data-evernote-id="590">'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0'</span> \
-H <span class="hljs-string js-evernote-checked" data-evernote-id="591">'X-Requested-With: XMLHttpRequest'</span> \
-H <span class="hljs-string js-evernote-checked" data-evernote-id="592">'Referer: https://domain.name'</span> \
-H <span class="hljs-string js-evernote-checked" data-evernote-id="593">'Cookie: <span class="hljs-variable js-evernote-checked" data-evernote-id="594">$cookie</span>'</span> \
-H <span class="hljs-string js-evernote-checked" data-evernote-id="595">'Connection: keep-alive'</span> \
-b <span class="hljs-string js-evernote-checked" data-evernote-id="596">'/tmp/cookie'</span> \
-c <span class="hljs-string js-evernote-checked" data-evernote-id="597">'/tmp/cookie'</span> \
-X POST -d <span class="hljs-string js-evernote-checked" data-evernote-id="598">'DATA'</span> \
-L <span class="hljs-string js-evernote-checked" data-evernote-id="599">'https://domain.name/url'</span> |gzip -dc

    • Linux修改时区

<span class="hljs-attribute js-evernote-checked" data-evernote-id="601">ln</span> -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo <span class="hljs-string js-evernote-checked" data-evernote-id="602">"Asia/Shanghai"</span> >/etc/timezone

    • 时间不对问题

apt-get <span class="hljs-keyword js-evernote-checked" data-evernote-id="604">install</span> -y ntpdate
ntpdate pool.ntp.org
hwclock <span class="hljs-comment js-evernote-checked" data-evernote-id="605">--systohc</span>

    • 修改PHP默认时间

<span class="hljs-attribute js-evernote-checked" data-evernote-id="607">sed</span> -i <span class="hljs-string js-evernote-checked" data-evernote-id="608">'s/^;\?date.timezone.*/date.timezone = PRC/g'</span> /etc/php5/cgi/php.ini

    • 使Debian支持中文字符,解决乱码问题.
      • 运行

dpkg-reconfigure locales
      • 选择

<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="613">en_US</span><span class="hljs-selector-class js-evernote-checked" data-evernote-id="614">.UTF-8</span>
<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="615">zh_CN</span> <span class="hljs-selector-tag js-evernote-checked" data-evernote-id="616">GB2312</span>
<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="617">zh_CN</span><span class="hljs-selector-class js-evernote-checked" data-evernote-id="618">.GBK</span> <span class="hljs-selector-tag js-evernote-checked" data-evernote-id="619">GBK</span>
<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="620">zh_CN</span><span class="hljs-selector-class js-evernote-checked" data-evernote-id="621">.UTF-8</span> <span class="hljs-selector-tag js-evernote-checked" data-evernote-id="622">UTF-8</span>
      • 选择默认

<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="624">en_US</span><span class="hljs-selector-class js-evernote-checked" data-evernote-id="625">.UTF-8</span>
      • 安装字体

apt-get <span class="hljs-keyword js-evernote-checked" data-evernote-id="627">install</span> -y xfonts-wqy ttf-wqy-zenhei ttf-wqy-microhei
      • 重启

<span class="hljs-attribute js-evernote-checked" data-evernote-id="629">reboot</span>

    • 使用scp命令在Linux主机间传送文件

scp -P $SSHPort -r /Local/Path $RemoteUserName@$RemoteIP<span class="hljs-symbol js-evernote-checked" data-evernote-id="631">:/Remote/Path</span>

    • 检测IP有效性

IP=1.2.3.4
CHECKIP="$(echo "$IP" | grep -o '\(\([<span class="hljs-string js-evernote-checked" data-evernote-id="633">0-9</span>]\|[<span class="hljs-string js-evernote-checked" data-evernote-id="634">1-9</span>][<span class="hljs-symbol js-evernote-checked" data-evernote-id="635">0-9</span>]\|1[<span class="hljs-string js-evernote-checked" data-evernote-id="636">0-9</span>]\{2\}\|2[<span class="hljs-string js-evernote-checked" data-evernote-id="637">0-4</span>][<span class="hljs-symbol js-evernote-checked" data-evernote-id="638">0-9</span>]\|25[<span class="hljs-string js-evernote-checked" data-evernote-id="639">0-5</span>]\)\.\)\{3\}\([<span class="hljs-string js-evernote-checked" data-evernote-id="640">0-9</span>]\|[<span class="hljs-string js-evernote-checked" data-evernote-id="641">1-9</span>][<span class="hljs-symbol js-evernote-checked" data-evernote-id="642">0-9</span>]\|1[<span class="hljs-string js-evernote-checked" data-evernote-id="643">0-9</span>]\{2\}\|2[<span class="hljs-string js-evernote-checked" data-evernote-id="644">0-4</span>][<span class="hljs-symbol js-evernote-checked" data-evernote-id="645">0-9</span>]\|25[0-5]\)')"
[ "$IP" == "$CHECKIP" ] && [ "$(echo "$IP" |awk -F '.' '{print NF}')" -eq '4' ] && echo "Ok! " || echo "Error! "

    • 利用openssl给nginx添加账户密码

echo -n <span class="hljs-string js-evernote-checked" data-evernote-id="647">'MoeClub.org:'</span> <span class="hljs-meta js-evernote-checked" data-evernote-id="648">>></span>/etc/nginx/password
openssl passwd Vicer <span class="hljs-meta js-evernote-checked" data-evernote-id="649">>></span>/etc/nginx/password

    • 非交互式更改Linux用户的密码

<span class="hljs-attribute js-evernote-checked" data-evernote-id="651">echo</span> -ne <span class="hljs-string js-evernote-checked" data-evernote-id="652">"asd123\nasd123\n"</span> |passwd root

    • 于iptables相关

#添加名为<span class="hljs-selector-attr js-evernote-checked" data-evernote-id="654">[NAME]</span>的<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="655">Chain</span>.
<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="656">iptables</span> <span class="hljs-selector-tag js-evernote-checked" data-evernote-id="657">-N</span> <span class="hljs-selector-attr js-evernote-checked" data-evernote-id="658">[NAME]</span>
#清空名为<span class="hljs-selector-attr js-evernote-checked" data-evernote-id="659">[NAME]</span>的<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="660">Chain</span>下所有规则.
<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="661">iptables</span> <span class="hljs-selector-tag js-evernote-checked" data-evernote-id="662">-N</span> <span class="hljs-selector-attr js-evernote-checked" data-evernote-id="663">[NAME]</span>
#删除名为<span class="hljs-selector-attr js-evernote-checked" data-evernote-id="664">[NAME]</span>的空<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="665">Chain</span>.
<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="666">iptables</span> <span class="hljs-selector-tag js-evernote-checked" data-evernote-id="667">-N</span> <span class="hljs-selector-attr js-evernote-checked" data-evernote-id="668">[NAME]</span>
    • iptables前面的规则会先生效,如果后面的规则与前面某条规则发生冲突,则后面的这条规则不会生效.

    • There is no public key available for the following key IDs

apt-get <span class="hljs-keyword js-evernote-checked" data-evernote-id="670">install</span> -y debian-keyring debian-<span class="hljs-keyword js-evernote-checked" data-evernote-id="671">archive</span>-keyring
apt-<span class="hljs-keyword js-evernote-checked" data-evernote-id="672">key</span> <span class="hljs-keyword js-evernote-checked" data-evernote-id="673">update</span>

    • 使用nc命令检测端口

<span class="hljs-selector-tag js-evernote-checked" data-evernote-id="675">nc</span> <span class="hljs-selector-tag js-evernote-checked" data-evernote-id="676">-z</span> <span class="hljs-selector-tag js-evernote-checked" data-evernote-id="677">-v</span> <span class="hljs-selector-tag js-evernote-checked" data-evernote-id="678">-w</span> 1 127<span class="hljs-selector-class js-evernote-checked" data-evernote-id="679">.0</span><span class="hljs-selector-class js-evernote-checked" data-evernote-id="680">.0</span><span class="hljs-selector-class js-evernote-checked" data-evernote-id="681">.1</span> 443
    • 可使用

$?

    • 来获取返回参数,0:open,1:close.

    • 使用sed调换两行的顺序

<span class="hljs-attribute js-evernote-checked" data-evernote-id="683">sed</span> <span class="hljs-string js-evernote-checked" data-evernote-id="684">'/lineONE/{h;d;};/LineTWO/G'</span>

    • 修改Linux系统时间

<span class="hljs-attribute js-evernote-checked" data-evernote-id="686">cp</span> -f /usr/share/zoneinfo/PRC /etc/localtime

    • ssh,scp无交互密码登录

<span class="hljs-attribute js-evernote-checked" data-evernote-id="688">sshpass</span> -p <span class="hljs-variable js-evernote-checked" data-evernote-id="689">$PASSWORD</span> ssh -o StrictHostKeyChecking=<span class="hljs-literal js-evernote-checked" data-evernote-id="690">no</span> -q -p <span class="hljs-variable js-evernote-checked" data-evernote-id="691">$PORT</span> root@<span class="hljs-variable js-evernote-checked" data-evernote-id="692">$IPADDR</span> <span class="hljs-string js-evernote-checked" data-evernote-id="693">"COMMAND;"</span>
sshpass -p <span class="hljs-variable js-evernote-checked" data-evernote-id="694">$PASSWORD</span> scp -P <span class="hljs-variable js-evernote-checked" data-evernote-id="695">$PORT</span> -o StrictHostKeyChecking=<span class="hljs-literal js-evernote-checked" data-evernote-id="696">no</span> -q -r <span class="hljs-variable js-evernote-checked" data-evernote-id="697">$FilePath</span> root@<span class="hljs-variable js-evernote-checked" data-evernote-id="698">$IPADDR</span>:<span class="hljs-variable js-evernote-checked" data-evernote-id="699">$FilePath</span>

    • 申请live后缀邮箱

<span class="hljs-comment js-evernote-checked" data-evernote-id="701"># 浏览器F12,粘贴即可使用</span>
var <span class="hljs-function js-evernote-checked" data-evernote-id="702"><span class="hljs-keyword js-evernote-checked" data-evernote-id="703">sub</span> = <span class="hljs-title js-evernote-checked" data-evernote-id="704">window</span>.<span class="hljs-title js-evernote-checked" data-evernote-id="705">prompt</span></span>('msn.com /live.com /live.cn /live.hk /live.jp /live.fr /live.ca /live.de /live.ie /live.in ...\n','live.cn');<span class="hljs-keyword js-evernote-checked" data-evernote-id="706">if</span>(<span class="hljs-function js-evernote-checked" data-evernote-id="707"><span class="hljs-keyword js-evernote-checked" data-evernote-id="708">sub</span>)</span>{document.getElementById(<span class="hljs-string js-evernote-checked" data-evernote-id="709">"SingleDomain"</span>).value = <span class="hljs-function js-evernote-checked" data-evernote-id="710"><span class="hljs-keyword js-evernote-checked" data-evernote-id="711">sub</span></span>;document.getElementById(<span class="hljs-string js-evernote-checked" data-evernote-id="712">"idSingleDomain"</span>).innerText = <span class="hljs-string js-evernote-checked" data-evernote-id="713">"@"+sub;};
</span>

    • 网易提供的检测是否为大陆IP接口

http:<span class="hljs-comment js-evernote-checked" data-evernote-id="715">//ipservice.163.com/isFromMainland</span>

    • bilibili检测IP接口

https:<span class="hljs-comment js-evernote-checked" data-evernote-id="717">//api.live.bilibili.com/ip_service/v1/ip_service/get_ip_addr</span>

    • sed多行合并

<span class="hljs-attribute js-evernote-checked" data-evernote-id="719">sed</span> -n <span class="hljs-string js-evernote-checked" data-evernote-id="720">'1h;1!H;<span class="hljs-variable js-evernote-checked" data-evernote-id="721">$g</span>;s/\n/,/g;<span class="hljs-variable js-evernote-checked" data-evernote-id="722">$p</span>'</span> /file/to/path

    • 指定时区查看时间

TZ=Asia/Tokyo date +<span class="hljs-string js-evernote-checked" data-evernote-id="724">"%Y.%m.%d %T"</span>

    • 去除重复行

<span class="hljs-attribute js-evernote-checked" data-evernote-id="726">cat</span> file |sort -nk2 |sed <span class="hljs-string js-evernote-checked" data-evernote-id="727">'$!N; /^\(.*\)\n\1$/!P; D'</span>

    • 利用awk去除重复行

<span class="hljs-attribute js-evernote-checked" data-evernote-id="729">awk</span> <span class="hljs-string js-evernote-checked" data-evernote-id="730">'!i[<span class="hljs-variable js-evernote-checked" data-evernote-id="731">$0</span>]++'</span> file

    • 解决Nginx 413错误

server {
    ...
    client_max_body_size 0;
    ...
}

    • 解决利用eval赋值后echo输出URLENCODE的暗坑

<span class="hljs-attribute js-evernote-checked" data-evernote-id="734">echo</span> <span class="hljs-string js-evernote-checked" data-evernote-id="735">"<span class="hljs-variable js-evernote-checked" data-evernote-id="736">$str</span>"</span> |sed <span class="hljs-string js-evernote-checked" data-evernote-id="737">'s/\+/%2B/g'</span> |sed <span class="hljs-string js-evernote-checked" data-evernote-id="738">'s/\=/%3D/g'</span> |sed <span class="hljs-string js-evernote-checked" data-evernote-id="739">'s/\//%2F/g'</span> |xxd -ps |sed <span class="hljs-string js-evernote-checked" data-evernote-id="740">'s/0d0a$\|0d$//g'</span> |xxd -r -ps

    • 自定义批量修改文件时间(格式:世纪年份月份天数小时分钟.秒数)

Time=<span class="hljs-string js-evernote-checked" data-evernote-id="742">"$(date +'%C%y%m%d%H%M.%S')"</span>; find . -name <span class="hljs-string js-evernote-checked" data-evernote-id="743">"*"</span> -<span class="hljs-keyword js-evernote-checked" data-evernote-id="744">exec</span> touch -t $Time <span class="hljs-string js-evernote-checked" data-evernote-id="745">'{}'</span> \;

    • 重复内容交集和并集

<span class="hljs-comment js-evernote-checked" data-evernote-id="747">#并集(重复的行只保留一份)</span>
cat file<span class="hljs-number js-evernote-checked" data-evernote-id="748">0</span> |<span class="hljs-keyword js-evernote-checked" data-evernote-id="749">sort</span> -t<span class="hljs-string js-evernote-checked" data-evernote-id="750">';'</span> -k1 |uniq > file
<span class="hljs-comment js-evernote-checked" data-evernote-id="751">#交集(只留下重复的行)</span>
cat file<span class="hljs-number js-evernote-checked" data-evernote-id="752">0</span> |<span class="hljs-keyword js-evernote-checked" data-evernote-id="753">sort</span> -t<span class="hljs-string js-evernote-checked" data-evernote-id="754">';'</span> -k1 |uniq -d > file
<span class="hljs-comment js-evernote-checked" data-evernote-id="755">#只留下非重复行</span>
cat file<span class="hljs-number js-evernote-checked" data-evernote-id="756">0</span> |<span class="hljs-keyword js-evernote-checked" data-evernote-id="757">sort</span> -t<span class="hljs-string js-evernote-checked" data-evernote-id="758">';'</span> -k1 |uniq -u > file

    • 利用awk进行计算

<span class="hljs-attribute js-evernote-checked" data-evernote-id="760">awk</span> <span class="hljs-string js-evernote-checked" data-evernote-id="761">'BEGIN{printf("%.2f\n",'</span><span class="hljs-variable js-evernote-checked" data-evernote-id="762">${RX_bytes}</span><span class="hljs-string js-evernote-checked" data-evernote-id="763">'/(1024*1024*1024))}'</span>;

    • MySQL数据库备份与恢复

<span class="hljs-comment js-evernote-checked" data-evernote-id="765">#备份数据</span>
<span class="hljs-attribute js-evernote-checked" data-evernote-id="766">mysqldump</span> -h <span class="hljs-variable js-evernote-checked" data-evernote-id="767">$host</span> -P <span class="hljs-variable js-evernote-checked" data-evernote-id="768">$port</span> -u<span class="hljs-string js-evernote-checked" data-evernote-id="769">"<span class="hljs-variable js-evernote-checked" data-evernote-id="770">$username</span>"</span> -p<span class="hljs-string js-evernote-checked" data-evernote-id="771">"<span class="hljs-variable js-evernote-checked" data-evernote-id="772">$password</span>"</span> --databases <span class="hljs-variable js-evernote-checked" data-evernote-id="773">$db_name</span> >dump.sql
<span class="hljs-comment js-evernote-checked" data-evernote-id="774">#还原数据</span>
mysql -h <span class="hljs-variable js-evernote-checked" data-evernote-id="775">$host</span> -P <span class="hljs-variable js-evernote-checked" data-evernote-id="776">$port</span> -u<span class="hljs-string js-evernote-checked" data-evernote-id="777">"<span class="hljs-variable js-evernote-checked" data-evernote-id="778">$username</span>"</span> -p<span class="hljs-string js-evernote-checked" data-evernote-id="779">"<span class="hljs-variable js-evernote-checked" data-evernote-id="780">$password</span>"</span> --databases <span class="hljs-variable js-evernote-checked" data-evernote-id="781">$db_name</span>
CREATE DATABASE IF NOT EXISTS <span class="hljs-variable js-evernote-checked" data-evernote-id="782">$db_name</span>;
<span class="hljs-attribute js-evernote-checked" data-evernote-id="783">SOURCE</span> dump.sql;
<span class="hljs-comment js-evernote-checked" data-evernote-id="784">#赋予权限</span>
<span class="hljs-attribute js-evernote-checked" data-evernote-id="785">grant</span> all privileges <span class="hljs-literal js-evernote-checked" data-evernote-id="786">on</span> <span class="hljs-variable js-evernote-checked" data-evernote-id="787">$db_name</span>.* to <span class="hljs-variable js-evernote-checked" data-evernote-id="788">$username</span>@<span class="hljs-variable js-evernote-checked" data-evernote-id="789">$host</span> identified by <span class="hljs-string js-evernote-checked" data-evernote-id="790">"<span class="hljs-variable js-evernote-checked" data-evernote-id="791">$password</span>"</span>;
<span class="hljs-comment js-evernote-checked" data-evernote-id="792">#刷新权限</span>
<span class="hljs-attribute js-evernote-checked" data-evernote-id="793">flush</span> privileges;
<span class="hljs-comment js-evernote-checked" data-evernote-id="794">#设置密码</span>
<span class="hljs-attribute js-evernote-checked" data-evernote-id="795">set</span> password for <span class="hljs-variable js-evernote-checked" data-evernote-id="796">$username</span>@<span class="hljs-variable js-evernote-checked" data-evernote-id="797">$host</span> = password(<span class="hljs-string js-evernote-checked" data-evernote-id="798">"<span class="hljs-variable js-evernote-checked" data-evernote-id="799">$password</span>"</span>);
<span class="hljs-comment js-evernote-checked" data-evernote-id="800">#删除数据库</span>
<span class="hljs-attribute js-evernote-checked" data-evernote-id="801">drop</span> database if exists <span class="hljs-variable js-evernote-checked" data-evernote-id="802">$db_name</span>;
<span class="hljs-comment js-evernote-checked" data-evernote-id="803">#创建用户</span>
<span class="hljs-attribute js-evernote-checked" data-evernote-id="804">create</span> user <span class="hljs-variable js-evernote-checked" data-evernote-id="805">$username</span>@<span class="hljs-variable js-evernote-checked" data-evernote-id="806">$host</span>;

    • shell取路径

<span class="hljs-comment js-evernote-checked" data-evernote-id="808">#path0=/root/test0/test1/test2</span>
<span class="hljs-comment js-evernote-checked" data-evernote-id="809">#path1=/root/test0/test1/</span>
<span class="hljs-comment js-evernote-checked" data-evernote-id="810">#path2=/home/test0/</span>

<span class="hljs-built_in js-evernote-checked" data-evernote-id="811">echo</span> <span class="hljs-variable js-evernote-checked" data-evernote-id="812">${path0%/*}</span>
~<span class="hljs-comment js-evernote-checked" data-evernote-id="813"># /root/test0/test1</span>
<span class="hljs-built_in js-evernote-checked" data-evernote-id="814">echo</span> <span class="hljs-variable js-evernote-checked" data-evernote-id="815">${path0/#$path1}</span>
~<span class="hljs-comment js-evernote-checked" data-evernote-id="816"># test2</span>
<span class="hljs-built_in js-evernote-checked" data-evernote-id="817">echo</span> <span class="hljs-variable js-evernote-checked" data-evernote-id="818">${path0/#$path1/$path2}</span>
~<span class="hljs-comment js-evernote-checked" data-evernote-id="819"># /home/test0/test2</span>

    • 文件描述符及简易通讯

<span class="hljs-comment js-evernote-checked" data-evernote-id="821">#打开可读写的文件描述符7</span>
<span class="hljs-attribute js-evernote-checked" data-evernote-id="822">exec</span> <span class="hljs-number js-evernote-checked" data-evernote-id="823">7</span><>/dev/tcp/<span class="hljs-number js-evernote-checked" data-evernote-id="824">127.0.0.1</span>/<span class="hljs-number js-evernote-checked" data-evernote-id="825">22</span> <span class="hljs-number js-evernote-checked" data-evernote-id="826">2</span>>/dev/null;
<span class="hljs-comment js-evernote-checked" data-evernote-id="827">#发送数据到文件描述符7</span>
<span class="hljs-attribute js-evernote-checked" data-evernote-id="828">echo</span> -e <span class="hljs-string js-evernote-checked" data-evernote-id="829">"ok \n"</span> >&<span class="hljs-number js-evernote-checked" data-evernote-id="830">7</span>;
<span class="hljs-comment js-evernote-checked" data-evernote-id="831">#从文件描述符7中获取返回的信息</span>
<span class="hljs-attribute js-evernote-checked" data-evernote-id="832">cat</span> <&<span class="hljs-number js-evernote-checked" data-evernote-id="833">7</span>;
<span class="hljs-comment js-evernote-checked" data-evernote-id="834">#关闭文件描述符7的输入和输出</span>
<span class="hljs-attribute js-evernote-checked" data-evernote-id="835">exec</span> <span class="hljs-number js-evernote-checked" data-evernote-id="836">7</span><&-;
<span class="hljs-attribute js-evernote-checked" data-evernote-id="837">exec</span> <span class="hljs-number js-evernote-checked" data-evernote-id="838">7</span>>&-;
<span class="hljs-comment js-evernote-checked" data-evernote-id="839">#建立的socket链接在 /proc/self/fd 目录下</span>
<span class="hljs-attribute js-evernote-checked" data-evernote-id="840">ls</span> -l /proc/self/fd

    • Debian 快速换内核

apt-get install -y linux-image-3.16.0.4-amd
item=<span class="hljs-string js-evernote-checked" data-evernote-id="842">"<span class="hljs-variable js-evernote-checked" data-evernote-id="843">${1:-3.16.0-4}</span>"</span>
<span class="hljs-keyword js-evernote-checked" data-evernote-id="844">while</span> [[ -n <span class="hljs-string js-evernote-checked" data-evernote-id="845">"<span class="hljs-variable js-evernote-checked" data-evernote-id="846">$(dpkg -l |grep linux-image |grep -v "$item" |awk '{print $2}')</span>"</span> ]]; <span class="hljs-keyword js-evernote-checked" data-evernote-id="847">do</span>
  <span class="hljs-keyword js-evernote-checked" data-evernote-id="848">for</span> kernel <span class="hljs-keyword js-evernote-checked" data-evernote-id="849">in</span> `dpkg -l |grep linux-image |grep -v <span class="hljs-string js-evernote-checked" data-evernote-id="850">"<span class="hljs-variable js-evernote-checked" data-evernote-id="851">$item</span>"</span> |awk <span class="hljs-string js-evernote-checked" data-evernote-id="852">'{print $2}'</span>`
    <span class="hljs-keyword js-evernote-checked" data-evernote-id="853">do</span>
      <span class="hljs-keyword js-evernote-checked" data-evernote-id="854">if</span> [ -f <span class="hljs-string js-evernote-checked" data-evernote-id="855">"/var/lib/dpkg/info/<span class="hljs-variable js-evernote-checked" data-evernote-id="856">${kernel}</span>.prerm"</span> ]; <span class="hljs-keyword js-evernote-checked" data-evernote-id="857">then</span>
        sed -i <span class="hljs-string js-evernote-checked" data-evernote-id="858">'s/uname -r/echo purge/'</span> <span class="hljs-string js-evernote-checked" data-evernote-id="859">"/var/lib/dpkg/info/<span class="hljs-variable js-evernote-checked" data-evernote-id="860">${kernel}</span>.prerm"</span>
      <span class="hljs-keyword js-evernote-checked" data-evernote-id="861">fi</span>
      dpkg --purge <span class="hljs-string js-evernote-checked" data-evernote-id="862">"<span class="hljs-variable js-evernote-checked" data-evernote-id="863">$kernel</span>"</span>
    <span class="hljs-keyword js-evernote-checked" data-evernote-id="864">done</span>
  <span class="hljs-keyword js-evernote-checked" data-evernote-id="865">done</span>

    • Redis关闭持久化

<span class="hljs-attribute js-evernote-checked" data-evernote-id="867">sed</span> -i <span class="hljs-string js-evernote-checked" data-evernote-id="868">'s/^save/#&/g'</span> /etc/redis/redis.conf
sed -i <span class="hljs-string js-evernote-checked" data-evernote-id="869">'s/^appendonly\ .*/appendonly\ no/g'</span> /etc/redis/redis.conf
rm -rf /var/lib/redis/*
赞(0)
未经允许不得转载:798VPS » Linux使用过程中的一些技巧
分享到: 更多 (0)