mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
20141023-1 选题
This commit is contained in:
parent
ede6104cf3
commit
8b350aed96
@ -0,0 +1,255 @@
|
||||
What are useful Bash aliases and functions
|
||||
================================================================================
|
||||
As a command line adventurer, you probably found yourself repeating the same lengthy commands over and over. If you always ssh into the same machine, if you always chain the same commands together, or if you constantly run a program with the same flags, you might want to save the precious seconds of your life that you spend repeating the same actions over and over.
|
||||
|
||||
The solution to achieve that is to use an alias. As you may know, an alias is a way to tell your shell to remember a particular command and give it a new name: an alias. However, an alias is quickly limited as it is just a shortcut for a shell command, without the ability to pass or control the arguments. So to complement, bash also allows you create your own functions, which can be more lengthy and complex, and also accepts any number of arguments.
|
||||
|
||||
Naturally, like with soup, when you have a good recipe you share it. So here is a list with some of the most useful bash aliases and functions. Note that "most useful" is loosely defined, and of course the usefulness of an alias is dependent on your everyday usage of the shell.
|
||||
|
||||
Before you start experimenting with aliases, here is a handy tip: if you give an alias the same name as a regular command, you can choose to launch the original command and ignore the alias with the trick:
|
||||
|
||||
\command
|
||||
|
||||
For example, the first alias below replaces the ls command. If you wish to use the regular ls command and not the alias, call it via:
|
||||
|
||||
\ls
|
||||
|
||||
### Productivity ###
|
||||
|
||||
So these aliases are really simple and really short, but they are mostly based on the idea that if you save yourself a fraction of a second every time, it might end up accumulating years at the end. Or maybe not.
|
||||
|
||||
alias ls="ls --color=auto"
|
||||
|
||||
Simple but vital. Make the ls command output in color.
|
||||
|
||||
alias ll = "ls --color -al"
|
||||
|
||||
Shortcut to display in color all the files from a directory in a list format.
|
||||
|
||||
alias grep='grep --color=auto'
|
||||
|
||||
Similarly, put some color in the grep output.
|
||||
|
||||
mcd() { mkdir -p "$1"; cd "$1";}
|
||||
|
||||
One of my favorite. Make a directory and cd into it in one command: mcd [name].
|
||||
|
||||
cls() { cd "$1"; ls;}
|
||||
|
||||
Similar to the previous function, cd into a directory and list its content: cls [name].
|
||||
|
||||
backup() { cp "$1"{,.bak};}
|
||||
|
||||
Simple way to make a backup of a file: backup [file] will create [file].bak in the same directory.
|
||||
|
||||
md5check() { md5sum "$1" | grep "$2";}
|
||||
|
||||
Because I hate comparing the md5sum of a file by hand, this function computes it and compares it using grep: md5check [file] [key].
|
||||
|
||||
![](https://farm6.staticflickr.com/5616/15412389280_8be57841ae_o.jpg)
|
||||
|
||||
alias makescript="fc -rnl | head -1 >"
|
||||
|
||||
Easily make a script out of the last command you ran: makescript [script.sh]
|
||||
|
||||
alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo"
|
||||
|
||||
Just to generate a strong password instantly.
|
||||
|
||||
![](https://farm4.staticflickr.com/3955/15574321206_dd365f0f0e.jpg)
|
||||
|
||||
alias c="clear"
|
||||
|
||||
Cannot do simpler to clean your terminal screen.
|
||||
|
||||
alias histg="history | grep"
|
||||
|
||||
To quickly search through your command history: histg [keyword]
|
||||
|
||||
alias ..='cd ..'
|
||||
|
||||
No need to write cd to go up a directory.
|
||||
|
||||
alias ...='cd ../..'
|
||||
|
||||
Similarly, go up two directories.
|
||||
|
||||
extract() {
|
||||
if [ -f $1 ] ; then
|
||||
case $1 in
|
||||
*.tar.bz2) tar xjf $1 ;;
|
||||
*.tar.gz) tar xzf $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 via extract()" ;;
|
||||
esac
|
||||
else
|
||||
echo "'$1' is not a valid file"
|
||||
fi
|
||||
}
|
||||
|
||||
Longest but also the most useful. Extract any kind of archive: extract [archive file]
|
||||
|
||||
### System Info ###
|
||||
|
||||
Want to know everything about your system as quickly as possible?
|
||||
|
||||
alias cmount="mount | column -t"
|
||||
|
||||
Format the output of mount into columns.
|
||||
|
||||
![](https://farm6.staticflickr.com/5603/15598830622_587b77a363_z.jpg)
|
||||
|
||||
alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'"
|
||||
|
||||
Display the directory structure recursively in a tree format.
|
||||
|
||||
sbs() { du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e';}
|
||||
|
||||
"Sort by size" to display in list the files in the current directory, sorted by their size on disk.
|
||||
|
||||
alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p"
|
||||
|
||||
Intercept the stdout and stderr of a process: intercept [some PID]. Note that you will need strace installed.
|
||||
|
||||
alias meminfo='free -m -l -t'
|
||||
|
||||
See how much memory you have left.
|
||||
|
||||
![](https://farm4.staticflickr.com/3955/15411891448_0b9d6450bd_z.jpg)
|
||||
|
||||
alias ps? = "ps aux | grep"
|
||||
|
||||
Easily find the PID of any process: ps? [name]
|
||||
|
||||
alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1"
|
||||
|
||||
Displays the current sound volume.
|
||||
|
||||
![](https://farm4.staticflickr.com/3939/15597995445_99ea7ffcd5_o.jpg)
|
||||
|
||||
### Networking ###
|
||||
|
||||
For all the commands that involve the Internet or your local network, there are fancy aliases for them.
|
||||
|
||||
alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla"
|
||||
|
||||
Download entirely a website: websiteget [URL]
|
||||
|
||||
alias listen="lsof -P -i -n"
|
||||
|
||||
Show which applications are connecting to the network.
|
||||
|
||||
![](https://farm4.staticflickr.com/3943/15598830552_c7e5eaaa0d_z.jpg)
|
||||
|
||||
alias port='netstat -tulanp'
|
||||
|
||||
Show the active ports
|
||||
|
||||
gmail() { curl -u "$1" --silent "https://mail.google.com/mail/feed/atom" | sed -e 's/<\/fullcount.*/\n/' | sed -e 's/.*fullcount>//'}
|
||||
|
||||
Rough function to display the number of unread emails in your gmail: gmail [user name]
|
||||
|
||||
alias ipinfo="curl ifconfig.me && curl ifconfig.me/host"
|
||||
|
||||
Get your public IP address and host.
|
||||
|
||||
getlocation() { lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\';}
|
||||
|
||||
Returns your current location based on your IP address.
|
||||
|
||||
### Useless ###
|
||||
|
||||
So what if some aliases are not all that productive? They can still be fun.
|
||||
|
||||
kernelgraph() { lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -;}
|
||||
|
||||
To draw the kernel module dependency graph. Requires image viewer.
|
||||
|
||||
alias busy="cat /dev/urandom | hexdump -C | grep "ca fe""
|
||||
|
||||
Make you look all busy and fancy in the eyes of non-technical people.
|
||||
|
||||
![](https://farm6.staticflickr.com/5599/15574321326_ab3fbc1ef9_z.jpg)
|
||||
|
||||
To conclude, a good chunk of these aliases and functions come from my personal .bashrc, and the awesome websites [alias.sh][1] and [commandlinefu.com][2] which I already presented in my post on the [best online tools for Linux][3]. So definitely go check them out, make your own recipes, and if you are so inclined, share your wisdom in the comments.
|
||||
|
||||
As a bonus, here is the plain text version of all the aliases and functions I mentioned, ready to be copy pasted in your bashrc.
|
||||
|
||||
#Productivity
|
||||
alias ls="ls --color=auto"
|
||||
alias ll="ls --color -al"
|
||||
alias grep='grep --color=auto'
|
||||
mcd() { mkdir -p "$1"; cd "$1";}
|
||||
cls() { cd "$1"; ls;}
|
||||
backup() { cp "$1"{,.bak};}
|
||||
md5check() { md5sum "$1" | grep "$2";}
|
||||
alias makescript="fc -rnl | head -1 >"
|
||||
alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo"
|
||||
alias c="clear"
|
||||
alias histg="history | grep"
|
||||
alias ..='cd ..'
|
||||
alias ...='cd ../..'
|
||||
extract() {
|
||||
if [ -f $1 ] ; then
|
||||
case $1 in
|
||||
*.tar.bz2) tar xjf $1 ;;
|
||||
*.tar.gz) tar xzf $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 via extract()" ;;
|
||||
esac
|
||||
else
|
||||
echo "'$1' is not a valid file"
|
||||
fi
|
||||
}
|
||||
|
||||
#System info
|
||||
alias cmount="mount | column -t"
|
||||
alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'"
|
||||
sbs(){ du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e';}
|
||||
alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p"
|
||||
alias meminfo='free -m -l -t'
|
||||
alias ps?="ps aux | grep"
|
||||
alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1"
|
||||
|
||||
#Network
|
||||
alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla"
|
||||
alias listen="lsof -P -i -n"
|
||||
alias port='netstat -tulanp'
|
||||
gmail() { curl -u "$1" --silent "https://mail.google.com/mail/feed/atom" | sed -e 's/<\/fullcount.*/\n/' | sed -e 's/.*fullcount>//'}
|
||||
alias ipinfo="curl ifconfig.me && curl ifconfig.me/host"
|
||||
getlocation() { lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\';}
|
||||
|
||||
#Funny
|
||||
kernelgraph() { lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -;}
|
||||
alias busy="cat /dev/urandom | hexdump -C | grep \"ca fe\""
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://xmodulo.com/useful-bash-aliases-functions.html
|
||||
|
||||
作者:[Adrien Brochard][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://xmodulo.com/author/adrien
|
||||
[1]:http://alias.sh/
|
||||
[2]:http://www.commandlinefu.com/commands/browse
|
||||
[3]:http://xmodulo.com/useful-online-tools-linux.html
|
@ -0,0 +1,115 @@
|
||||
What is a good command-line calculator on Linux
|
||||
================================================================================
|
||||
Every modern Linux desktop distribution comes with a default GUI-based calculator app. On the other hand, if your workspace is full of terminal windows, and you would rather crunch some numbers within one of those terminals quickly, you are probably looking for a **command-line calculator**. In this category, [GNU bc][1] (short for "basic calculator") is a hard to beat one. While there are many command-line calculators available on Linux, I think GNU bc is hands-down the most powerful and useful.
|
||||
|
||||
Predating the GNU era, bc is actually a historically famous arbitrary precision calculator language, with its first implementation dating back to the old Unix days in 1970s. Initially bc was a better known as a programming language whose syntax is similar to C language. Over time the original bc evolved into POSIX bc, and then finally GNU bc of today.
|
||||
|
||||
### Features of GNU bc ###
|
||||
|
||||
Today's GNU bc is a result of many enhancements of earlier implementations of bc, and now it comes standard on all major GNU/Linux distros. It supports standard arithmetic operators with arbitrary precision numbers, and multiple numeric base (e.g., binary, decimal hexadecimal) of input and output.
|
||||
|
||||
If you are familiar with C language, you will see that the same or similar mathematical operators are used in bc. Some of supported operators include arithmetic (+,-,*,/,%,++,--), comparison (<,>,==,!=,<=,>=), logical (!,&&,||), bitwise (&,|,^,~,<<,>>), compound assignment (+=,-=,*=,/=,%=,&=,|=,^=,&&=,||=,<<=,>>=) operators. bc comes with many useful built-in functions such as square root, sine, cosine, arctangent, natural logarithm, exponential, etc.
|
||||
|
||||
### How to Use GNU bc ###
|
||||
|
||||
As a command-line calculator, possible use cases of GNU bc are virtually limitless. In this tutorial, I am going to describe a few popular features of bc command. For a complete manual, refer to the [official source][2].
|
||||
|
||||
Unless you have a pre-written bc script, you typically run bc in interactive mode, where any typed statement or expression terminated with a newline is interpreted and executed on the spot. Simply type the following to enter an interactive bc session. To quit a session, type 'quit' and press Enter.
|
||||
|
||||
$ bc
|
||||
|
||||
![](https://farm4.staticflickr.com/3939/15403325480_d0db97d427_z.jpg)
|
||||
|
||||
The examples presented in the rest of the tutorial are supposed to be typed inside a bc session.
|
||||
|
||||
### Type expressions ###
|
||||
|
||||
To calculate an arithmatic expression, simply type the expression at the blinking cursor, and press Enter. If you want, you can store an intermediate result to a variable, then access the variable in other expressions.
|
||||
|
||||
![](https://farm6.staticflickr.com/5604/15403325460_b004b3f8da_o.png)
|
||||
|
||||
Within a given session, bc maintains a unlimited history of previously typed lines. Simply use UP arrow key to retrieve previously typed lines. If you want to limit the number of lines to keep in the history, assign that number to a special variable named history. By default the variable is set to -1, meaning "unlimited."
|
||||
|
||||
### Switch input/output base ###
|
||||
|
||||
Often times you want to type input expressions and display results in binary or hexadecimal formats. For that, bc allows you switch the numeric base of input or output numbers. Input and output bases are stored in ibase and obase, respectively. The default value of these special variables is 10, and valid values are 2 through 16 (or the value of BC_BASE_MAX environment variable in case of obase). To switch numeric base, all you have to do is to change the values of ibase and obase. For example, here are examples of summing up two hexadecimal/binary numbers:
|
||||
|
||||
![](https://farm6.staticflickr.com/5604/15402320019_f01325f199_z.jpg)
|
||||
|
||||
Note that I specify obase=16 before ibase=16, not vice versa. That is because if I specified ibase=16 first, the subsequent obase=16 statement would be interpreted as assigning 16 in base 16 to obase (i.e., 22 in decimal), which is not what we want.
|
||||
|
||||
### Adjust precision ###
|
||||
|
||||
In bc, the precision of numbers is stored in a special variable named scale. This variable represents the number of decimal digits after the decimal point. By default, scale is set to 0, which means that all numbers and results and truncated/stored in integers. To adjust the default precision, all you have to do is to change the value of scale variable.
|
||||
|
||||
scale=4
|
||||
|
||||
![](https://farm6.staticflickr.com/5597/15586279541_211312597b.jpg)
|
||||
|
||||
### Use built-in functions ###
|
||||
|
||||
Beyond simple arithmatic operations, GNU bc offers a wide range of advanced mathematical functions built-in, via an external math library. To use those functions, launch bc with "-l" option from the command line.
|
||||
|
||||
Some of these built-in functions are illustrated here.
|
||||
|
||||
Square root of N:
|
||||
|
||||
sqrt(N)
|
||||
|
||||
Sine of X (X is in radians):
|
||||
|
||||
s(X)
|
||||
|
||||
Cosine of X (X is in radian):
|
||||
|
||||
c(X)
|
||||
|
||||
Arctangent of X (The returned value is in radian):
|
||||
|
||||
a(X)
|
||||
|
||||
Natural logarithm of X:
|
||||
|
||||
l(X)
|
||||
|
||||
Exponential function of X:
|
||||
|
||||
e(X)
|
||||
|
||||
### Other goodies as a language ###
|
||||
|
||||
As a full-blow calculator language, GNU bc supports simple statements (e.g., variable assignment, break, return), compound statements (e.g., if, while, for loop), and custom function definitions. I am not going to cover the details of these features, but you can easily learn how to use them from the [official manual][2]. Here is a very simple function definition example:
|
||||
|
||||
define dummy(x){
|
||||
return(x * x);
|
||||
}
|
||||
dummy(9)
|
||||
81
|
||||
dummy(4)
|
||||
16
|
||||
|
||||
### Use GNU bc Non-interactively ###
|
||||
|
||||
So far we have used bc within an interactive session. However, quite popular use cases of bc in fact involve running bc within a shell script non-interactively. In this case, you can send input to bc using echo through a pipe. For example:
|
||||
|
||||
$ echo "40*5" | bc
|
||||
$ echo "scale=4; 10/3" | bc
|
||||
$ echo "obase=16; ibase=2; 11101101101100010" | bc
|
||||
|
||||
![](https://farm4.staticflickr.com/3943/15565252976_f50f453c7f_z.jpg)
|
||||
|
||||
To conclude, GNU bc is a powerful and versatile command-line calculator that really lives up to your expectation. Preloaded on all modern Linux distributions, bc can make your number crunching tasks much easy to handle without leaving your terminals. For that, GNU bc should definitely be in your productivity toolset.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://xmodulo.com/command-line-calculator-linux.html
|
||||
|
||||
作者:[Dan Nanni][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://xmodulo.com/author/nanni
|
||||
[1]:http://www.gnu.org/software/bc/
|
||||
[2]:https://www.gnu.org/software/bc/manual/bc.html
|
Loading…
Reference in New Issue
Block a user