Bash CookBook (1)-basics, bashcookbook

Source: Internet
Author: User
Tags sigint signal

Bash CookBook (1)-basics, bashcookbook

Bash was written by brian Fox in January 10, 1988 out of Richard Stallman's suggestion.

 

1. Run the template:
  • Interactive Login shell, bash reads and executes/etc/profile after login.
  • Then read ~ /. Bash_profile ,~ /. Bash_login ,~ /. Profile, the first executable is found to be executed. When you log out, it will read and execute ~ /. Bash_logout.
  • Interactive non-login startup will read and execute ~ /. Bashrc.
  • Bash_history stores the run cmd.
Purpose: before and after execution, users are allowed to mount custom logic, which can be customized by each user.

 

Ii. Shortcut Keys:
  • Mobile

Ctrl + a: move the cursor to the starting position of the row.

Ctrl + e: move the cursor to the end of the row.

Alt + f: move the cursor forward in Word Form

Alt + B: move the cursor backward in Word Form

  • Signal type

Ctrl + c: Send a SIGINT signal to the current task to stop and exit.

Ctrl + z: Send the signal SIGTSTP to the foreground task to suspend it.

  • Editing

Tab: auto-completion

Ctrl + k: Delete the character after the cursor and copy it to the clipboard.

Ctrl + u: Delete the character before the cursor and copy it to the clipboard.

Ctrl + y: copy the content of the clipboard and insert the cursor position

Ctrl + _: roll back and modify.

Ctrl + t: two characters before the cursor is exchanged.

 

Iii. execution model:

 

The Shell execution environment is a theater. The theater has a foreground and background. There are many repertoire that can be performed on the stage, and there is only one repertoire (fg task) on the stage) after being admired, other plays are arranged in the background (bg task) until the user wants to see it and replace the current play (fg, bg command ).

 

2. Rules:

  • At a certain time point, only one command is executed in foreground, and the Shell control is returned to the user only after the foreground command is executed.
  • The command is followed by &, indicating that the background command is executed without affecting user interaction. You can also execute other commands.
  • In the Command run by fg, press ctrl + z to run its suspend. Suspend command again using fg and bg.

 

3. confusions:

 

How many commands are executed to complete a job? You can combine the commands into a script file for execution.

 

Iv. Script syntax
  • Brace expansion a {B, c, d} e extended to 'abe ace ad'
  • Tilde expansion
  • Parameter expansion
  • Command substitution

2. Quoting: There are expanish and quoting. quoting has three types:

  • Escape character: retains the literal meaning of the character that follows it.
  • Single quotes: retain the literal meaning of single quotes. Single quotes cannot appear in single quotes even if they are escaped.
  • Double quotation marks: Keep double quotation marks except $ (parameter expanision ),',\,! The literal meaning of a foreign character.
V. Example

 

The Startup File of Cron (a service for executing scheduled tasks)

/Etc/init. d/crond

 

   1: #! /bin/bash
   2: #
   3: # crond          Start/Stop the cron clock daemon.
   4: #
   5: # chkconfig: 2345 90 60
   6: # description: cron is a standard UNIX program that runs user-specified \
   7: #              programs at periodic scheduled times. vixie cron adds a \
   8: #              number of features to the basic UNIX cron, including better \
   9: #              security and more powerful configuration options.
  10: # processname: crond
  11: # config: /etc/crontab  
  12: # pidfile: /var/run/crond.pid
  13:  
  14: # Source function library.
  15: . /etc/init.d/functions
  16: . /etc/sysconfig/crond
  17: t=${CRON_VALIDATE_MAILRCPTS:-UNSET}
  18: [ "$t" != "UNSET" ] &;& export CRON_VALIDATE_MAILRCPTS="$t"
  19:  
  20:  # See how we were called.
  21:  
  22:  prog="crond"
  23:  
  24:  start() {
  25:       echo -n $"Starting $prog: "
  26:           if [ -e /var/lock/subsys/crond ]; then
  27:           if [ -e /var/run/crond.pid ] &;& [ -e /proc/`cat /var/run/crond.pid`     ]; then
  28:           echo -n $"cannot start crond: crond is already running.";
  29:           failure $"cannot start crond: crond already running.";
  30:           echo
  31:           return 1
  32:           fi
  33:      fi
  34:       daemon crond $CRONDARGS
  35:       RETVAL=$?
  36:       echo
  37:       [ $RETVAL -eq 0 ] &;& touch /var/lock/subsys/crond;
  38:       return $RETVAL
  39:   }

 

  • First line #! /Bin/bash indicates that the interpreter of the current script is bash.
  • Line 2-14 is comment ).
  • The 15,16 part indicates loading and executing the corresponding script.
  • The second line is a classic shell statement, which is very concise and called List, separated by &,;, &, |. The general language format is as follows:

If ("$ t "! = "UNSET ")

Export CRON_VALIDATE_MAILRCPTS = "$ t"

"" Indicates a quote that allows prameter expanision to be executed. Replace the value of t with $ t.

  • Row 24th start () {} indicates the Function Definition
  • Line 26-33 checks the existence of the file lock. If yes, checks the existence of the pid file and process. If yes, exits.
  • Line 3 starts crond
  • Row 3 RETVAL =$? $? It indicates the status of the newly executed foreground command, that is, the returned result of the crond startup process.
  • If the value of row 36th is 0, the instance is started successfully. The result is returned when the lock status is updated.

Who can recommend an entry-level book on shell script programming in linux? In addition to the entry-level books, it is best to recommend a further-level book.

Linux and UNIX Shell programming guide
Ishare.iask.sina.com.cn/f/10293091.html
I personally think it's a good book.

I am a beginner in Linux. I recommend a basic book for Linux.

If you want to buy a book, we recommend that you buy the operating system principles instead of getting started with linux.
Learning linux requires a lot of hardware knowledge, otherwise it will be difficult to go deep.

Of course, if you are familiar with the command, you can manage the system.

Practice has proved that you are not familiar with operating system principles when you are not in front of your computer. It's all about theoretical knowledge, because it's hard for you to practice hardware without financial resources or time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.