這是一個系列文章,主要分享shell(部分功能僅適用於bash)的使用建議和技巧,每次分享3點,希望你能有所收穫。
1 交互模式時自動輸入
$ pip uninstall redis
Uninstalling redis-2.10.6:
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/DESCRIPTION.rst
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/INSTALLER
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/METADATA
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/RECORD
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/WHEEL
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/metadata.json
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/top_level.txt
/usr/lib/python2.7/site-packages/redis/__init__.py
/usr/lib/python2.7/site-packages/redis/__init__.pyc
/usr/lib/python2.7/site-packages/redis/_compat.py
/usr/lib/python2.7/site-packages/redis/_compat.pyc
/usr/lib/python2.7/site-packages/redis/client.py
/usr/lib/python2.7/site-packages/redis/client.pyc
/usr/lib/python2.7/site-packages/redis/connection.py
/usr/lib/python2.7/site-packages/redis/connection.pyc
/usr/lib/python2.7/site-packages/redis/exceptions.py
/usr/lib/python2.7/site-packages/redis/exceptions.pyc
/usr/lib/python2.7/site-packages/redis/lock.py
/usr/lib/python2.7/site-packages/redis/lock.pyc
/usr/lib/python2.7/site-packages/redis/sentinel.py
/usr/lib/python2.7/site-packages/redis/sentinel.pyc
/usr/lib/python2.7/site-packages/redis/utils.py
/usr/lib/python2.7/site-packages/redis/utils.pyc
Proceed (y/n)? y
Successfully uninstalled redis-2.10.6
$ pip install redis
Collecting redis
Using cached redis-2.10.6-py2.py3-none-any.whl
Installing collected packages: redis
Successfully installed redis-2.10.6
$ echo y | pip uninstall redis
Uninstalling redis-2.10.6:
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/DESCRIPTION.rst
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/INSTALLER
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/METADATA
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/RECORD
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/WHEEL
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/metadata.json
/usr/lib/python2.7/site-packages/redis-2.10.6.dist-info/top_level.txt
/usr/lib/python2.7/site-packages/redis/__init__.py
/usr/lib/python2.7/site-packages/redis/__init__.pyc
/usr/lib/python2.7/site-packages/redis/_compat.py
/usr/lib/python2.7/site-packages/redis/_compat.pyc
/usr/lib/python2.7/site-packages/redis/client.py
/usr/lib/python2.7/site-packages/redis/client.pyc
/usr/lib/python2.7/site-packages/redis/connection.py
/usr/lib/python2.7/site-packages/redis/connection.pyc
/usr/lib/python2.7/site-packages/redis/exceptions.py
/usr/lib/python2.7/site-packages/redis/exceptions.pyc
/usr/lib/python2.7/site-packages/redis/lock.py
/usr/lib/python2.7/site-packages/redis/lock.pyc
/usr/lib/python2.7/site-packages/redis/sentinel.py
/usr/lib/python2.7/site-packages/redis/sentinel.pyc
/usr/lib/python2.7/site-packages/redis/utils.py
/usr/lib/python2.7/site-packages/redis/utils.pyc
Proceed (y/n)? Successfully uninstalled redis-2.10.6
有時候執行命令會進入交互模式,需要根據輸入作出不同響應。如果我們提前知道需要輸入的字符沒有風險,那麼可以通過echo命令將字符直接傳遞給命令,避免進入交互模式耽擱時間。比如示例中通過pip命令卸載redis包時,就會讓用戶輸入y或者n,然後根據輸入進行下一步操作。藉助echo命令,可以避免進入交互模式,命令快速執行完畢。再次提醒,前提是你知道輸入的字符沒有風險,比如不要隨便給rm命令echo一個y。
2 運行c代碼
$ cat helloworld.c
#include <stdio.h>
int main ()
{
printf("hello world!\n");
return 0;
}
$ gcc helloworld.c -o helloworld
$ ./helloworld
hello world!
$ ls | grep helloworld
helloworld
helloworld.c
$ crn helloworld.c
hello world!
$ ls | grep helloworld
helloworld
helloworld.c
定義的crn函數如下:
crn(){
gcc $* -g;
if [ $? -ne 0 ]; then
echo "failed to build $1"
else
./a.out
rm -rf ./a.out
fi
}
在linux命令行,要運行一個c程序,需要先編譯c代碼,然後才能運行該程序。使用go語言運行go代碼時,發現go可以直接運行源代碼,於是有所啓示,我定義一個crn函數,該函數參數是c源代碼文件,crn命令會先編譯c代碼,然後執行編譯後的可執行程序,執行完畢後將可執行程序刪除,省去了編譯環節,更加快速的執行c源程序。例如示例中,執行crn helloworld.c命令就可以運行該c源代碼,確實方便不少。
3 解壓常見格式的壓縮包
ltar(){
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar xjf $1;;
*.tar.gz) tar zxvf $1;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z)7z x $1 ;;
*) echo "'$1' cannot be extracted";;
esac
else
echo "'$1' is not a valid file"
fi
}
$ ls
gz_file.tar.gz tar_file.tar
$ ltar tar_file.tar
$ ltar gz_file.tar.gz
日常工作中,經常需要解壓不同格式的壓縮包,但是壓縮格式有很多種,每種壓縮格式的解壓選項和命令行有可能不一樣,要記住那麼多解壓命令和選項實在不容易。通過定義一個函數ltar,將常見壓縮格式文件的解壓命令都封裝在該函數內部,實現一鍵解壓,例如示例中執行ltar tar_file.tar命令和ltar gz_file.tar.gz命令都能解壓相應的壓縮文件。