Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Bash Shell

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use Bash Shell". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Bash Shell.

As a command-line explorer, you may find yourself repeating the same commands over and over again. If you always use ssh to get into the same computer, if you always connect a series of commands, if you always run a program with the same parameters, you may want to save your life a few seconds in this constant repetition.

The solution is to use an alias (alias). As you probably know, aliases are a way to make your shell remember a particular command and give it a new name. In any case, aliases have some limitations; they are just shortcuts to shell commands and cannot be passed or controlled. So as a supplement, bash also allows you to create your own functions, which may be longer and more complex, and it allows any number of arguments.

Of course, when you have good food, such as some kind of soup, you have to share it with everyone. I have a list of some of the most useful bash aliases and functions. Note that "the most useful" is just a term. Whether an alias is useful depends on whether you need to use it in shell every day.

Before you start your alias experience, here's an easy-to-use tip: if your alias is the same as the original command name, you can access the original command by using the following technique (LCTT translation note: you can also access it directly from the full path of the original command. )

\ command

For example, if you have an alias ls that replaces the ls command. If you want to use the original ls command instead of an alias, call it:

\ ls

Increase productivity

These aliases are really simple and really short, but most of them are designed to save your life a few seconds, maybe a few years in your life, maybe.

Alias ls= "ls-color=auto"

Simple but very important. Make the ls command output in color.

Alias ll= "ls-- color-al"

List all the files in the directory in a colored list.

Alias grep='grep-color=auto'

Similar, except that the output is colored in grep.

Mcd () {mkdir-p "$1"; cd "$1";}

One of my favorites. Create a directory and enter it: mcd [directory name].

Cls () {cd "$1"; ls;}

Similar to the previous function, enter a directory and list its contents: cls [directory name].

Backup () {cp "$1" {, .bak};}

Simply create a backup of the file: backup [file] will create [file] .bak in the same directory.

Md5check () {md5sum "$1" | grep "$2";}

Because I hate comparing the md5 check value of a file manually, this function calculates it and compares it: md5check [file] [check value].

Alias makescript= "fc-rnl | head-1 >"

It's easy to create a script with the last command you ran: makescript [script name .sh]

Alias genpasswd= "strings / dev/urandom | grep-o'[[: alnum:]]'| head-n 30 | tr-d'\ nkeeper; echo"

It's just an instant to produce a strong password.

Alias c = "clear"

Can't it be easier to clear your terminal screen?

Alias histg= "history | grep"

Quick search for your command input history: histg [keyword]

Alias.. ='cd..'

Do I need to enter cd to return to the upper directory?

Alias. ='cd.. /..'

Naturally, go to the upper two-tier directory.

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}

It's long, but also the most useful. Extract any document type: extract: [zip file]

System information

Want to know all the information about your system as soon as possible?

Alias cmount= "mount | column-t"

Output mount information in column format.

Alias tree= "ls-R | grep": $"| sed-e's pedigree / [^ -] [^\ /] *\ / /-- / g'- e's / ^ /'- e's Universe /'- e's Universe / | /'"

The directory structure is displayed recursively in a tree structure.

Sbs () {du-b-- max-depth 1 | sort-nr | perl-pe's {([0-9] +)} {sprintf "% .1f% s", $1 > = 2 cycles 30, "G"): $1 > = 2 cycles 20? ($1 pedigree 20, "M"): $1 > = 2 cycles 10? ($1 pedigree 10, "K"): ($1, ")} eBay;}

The installation files are sorted by the size of the disk storage, displaying a list of files in the current directory.

Alias intercept= "sudo strace-ff-e trace=write-e write=1,2-p"

Take over the standard output and standard error of a process. Note that you need to install strace.

Alias meminfo='free-m-l-t'

See how much memory you have left.

Alias ps? = "ps aux | grep"

Can you easily find the PID:ps of a process? [name].

Alias volume= "amixer get Master | sed '1Magazine 4d' | cut-d [- f 2 | cut-d]-f 1"

Displays the current volume settings.

The network

There are also some magic aliases for all commands used on the Internet and local networks.

Alias websiteget= "wget-- random-wait-r-p-e robots=off-U mozilla"

Download the entire website: websiteget [URL].

Alias listen= "lsof-P-I-n"

Shows which application is connected to the network.

Alias port='netstat-tulanp'

Shows the active port.

Gmail () {curl-u "$1"-- silent "https://mail.google.com/mail/feed/atom" | sed-e's Unimax'}

Roughly show the number of unread messages in your Google email: gmail [user name]

Alias ipinfo= "curl ifconfig.me & & curl ifconfig.me/host"

Get your public network IP address and hostname.

Getlocation () {lynx-dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address | egrep 'city | state | country' | awk' {print $3 My\\;}

Return the geographic location of your current IP address.

May be useless.

So, what if some aliases are not all useful? They may still be interesting.

Kernelgraph () {lsmod | perl-e'print "digraph\" lsmod\ "{"; while () {@ _ = split/\ shands; print "\" $_ [0]\ "- >\" $_\ "\ n" for split/,/,$_ [3]} print "}"'| dot-Tpng | display -;}

Draw the kernel module dependency graph. You can view the picture if you need it.

Alias busy= "cat / dev/urandom | hexdump-C | grep'ca fe'"

You always seem so busy and mysterious to those who are not technical.

As a reward, here are plain text versions of all the aliases and functions I mentioned, which can be copied and pasted into your .bashrc at any time. (if you have copied this line by line, , you find that you have wasted a few seconds of your life.)

# 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 sed'- e's'/ [^ -] [^\ /] *\ /-- / g'- e's / ^ / /'- e's hand / | /'" sbs () {du- B-- max-depth 1 | sort-nr | perl-pe's {([0-9] +)} {sprintf "% .1f% s" $1 > = 2pm 30? ($1max 2pm 30, "G"): $1 > = 2pm 20? ($1x 2pm 20, "M"): $1 > = 2pm 10? ($1max 2pm 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'1J 4d' | 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 lynx lynx'} alias ipinfo=" curl ifconfig.me & & curl ifconfig.me/host "getlocation () {lynx-dump lynx address | egrep 'city | state | country' | awk' {print $3 | sed's\ ip address flag\\'| sed's\ My\\' } # Funny kernelgraph () {lsmod | perl-e'print "digraph\" lsmod\ "{"; while () {@ _ = split/\ spocket; print "\" $_ [0]\ "- >\" $_\ "\ n" for split/,/,$_ [3]} print "}"'| dot-Tpng | display- } alias busy= "cat / dev/urandom | hexdump-C | grep\" ca fe\ "" so far, I believe you have a better understanding of "how to use Bash Shell". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report