Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Linux Shell Understanding and Learning     - CentOS7 + Redis Live Installation and Configuration (Linux)

- Timing Nginx logs cut and remove the log records of the specified number of days before (Server)

- ImageMagick Tutorial: How to cut images in Linux command line (Linux)

- Sleuth Kit: used to analyze a disk image and restore files open source forensics tools (Linux)

- SQL MySQL query table duplicate data (Database)

- MySQL and Oracle time zone settings compare (Database)

- Ubuntu 15.04 installed JDK and configured as the default JDK (Linux)

- How to migrate MySQL to MariaDB under linux (Database)

- Migu online music player for Linux (Linux)

- Git / Github use notes (Linux)

- Httpclient4.4 of principle (Http execution context) (Programming)

- To generate a certificate using OpenSSL under Linux (Server)

- The difference between Linux su and sudo commands (Linux)

- Compare Swift achieve rapid sorting and sorted Methods (Programming)

- Google open source TCP team repaired the Linux kernel TCP flaw (Linux)

- ORA-27054 NFS problem solving (Database)

- To install the Contiki development toolchain on Ubuntu (Linux)

- ApacheDS configuration of users and user groups to achieve SSO (Server)

- Transfer files to Windows and Linux (Linux)

- Cancel Root Command History under Linux (Linux)

 
         
  Linux Shell Understanding and Learning
     
  Add Date : 2017-01-08      
         
         
         
  Run the way Linux terminal is handled by bash environment. bash includes set up and use variables , Construction bash operating environment, the data stream redirection function. The following knowledge, maintenance and management of a host of important help.

Manage the entire computer hardware is actually an operating system kernel, the kernel is protected only by general users to shell and core communications (communications command shell entered with the kernel, which can control the hardware to work correctly). man, chmod, chown, vi, mkfs command, etc., are independent of the application, we can (ie, command-line mode) operated by shell these applications, so that these applications call the kernel to run the required work.

bash shell built-in commands: type

type command to find the executable file, rather than the general file name.
linuxhost @ Ubuntu: ~ / Desktop / linux_study $ type ls # parameters did not add any time to list the most important usage of ls
ls is aliased to `ls --color = auto '
linuxhost @ ubuntu: ~ / Desktop / linux_study $ according to type -t ls # ls add parameters -t lists the time of execution
alias
linuxhost @ ubuntu: ~ / Desktop / linux_study $ type -a ls # -a will add parameters from the PATH variable definition will contain all of the commands ls are listed, including alias
ls is aliased to `ls --color = auto '
ls is / bin / ls
linuxhost @ ubuntu: ~ / Desktop / linux_study $ type cd
cd is a shell builtin # cd is a shell built-in commands

Command execution

Many times the input command is too long, will be added before the line break \, do so for the purpose of newline characters [enter] transfer over. After successful transfer, the next line will be the head> symbol, you can continue to enter commands.

shell variable function

Variable is to use a specific string representing not fixed content. In linux content to the variable is displayed, you can use the echo command to display this variable, but a variable when it is displayed, you must add $.
linuxhost @ ubuntu: ~ / Desktop / linux_study $ echo $ variable

linuxhost @ ubuntu: ~ / Desktop / linux_study $ echo $ PATH
/ Usr / local / sbin: / usr / local / bin: / usr / sbin: / usr / bin: / sbin: / bin: / usr / games: / usr / local / games

Set the variable
linuxhost @ ubuntu: ~ / Desktop / linux_study $ echo $ myname

linuxhost @ ubuntu: ~ / Desktop / linux_study $ myname = flora
linuxhost @ ubuntu: ~ / Desktop / linux_study $ echo $ myname
flora

Retain the original variable and then set
linuxhost @ ubuntu: ~ / Desktop / linux_study $ myname = "$ myname lin"
linuxhost @ ubuntu: ~ / Desktop / linux_study $ echo $ myname
flora lin

During execution, the anti-apostrophe ( `), within the command will be executed first, and the result will come out to perform as an external input information. locate command lists all the file names, but if we want to know the relevant permissions for each crontab file name:
linuxhost @ ubuntu: ~ / Desktop / linux_study $ ls -l `locate crontab`
-rw-r - r-- 1 root root 401 Feb 19 2014 / etc / anacrontab
-rw-r - r-- 1 root root 722 Feb 8 2013 / etc / crontab
-rwxr-sr-x 1 root crontab 34824 Feb 8 2013 / usr / bin / crontab
-rw-r - r-- 1 root root 1184 Apr 7 2014 / usr / share / bash-completion / completions / crontab
-rw-r - r-- 1 root root 27684 Feb 8 2013 /usr/share/doc/cron/examples/crontab2english.pl
-rw-r - r-- 1 root root 2521 Feb 8 2013 /usr/share/man/man1/crontab.1.gz
-rw-r - r-- 1 root root 892 Feb 19 2014 /usr/share/man/man5/anacrontab.5.gz
-rw-r - r-- 1 root root 5687 Feb 8 2013 /usr/share/man/man5/crontab.5.gz

First to locate the data file name listed, then use the ls command to deal with.

Function environment variables

Environment variables can help us achieve a lot of functions, including conversion main folder prompt display, perform the file lookup paths.

View the default environment variables:
linuxhost @ ubuntu: ~ / Desktop / linux_study $ env
# ......
_ = / Last parameter usr / bin / env # last used commands (or command itself)

random number

RANDOM variable content of between 0 and 32767, so the numerical calculation in order to obtain a single digit.
linuxhost @ ubuntu: ~ / Desktop / linux_study $ declare -i number = $ RANDOM * 10/32768; echo $ number
1
linuxhost @ ubuntu: ~ / Desktop / linux_study $ declare -i number = $ RANDOM * 10/32768; echo $ number
4
linuxhost @ ubuntu: ~ / Desktop / linux_study $ declare -i number = $ RANDOM * 10/32768; echo $ number
5

See all set variables (including environment variables and custom variables)

bash is not only the environment variables, as well as some of the more important parameters, as well as user-defined variables.
linuxhost @ ubuntu: ~ / Desktop / linux_study $ set

PS1 command prompt

Every time we press the Enter key to execute a command, the last prompt to appear again, will take the initiative to read the value of this variable.
linuxhost @ ubuntu: ~ / Desktop / linux_study $ cd / home
linuxhost @ ubuntu: / home $ PS1 = '[\ u @ \ h \ w \ A # \ #] \ $'
[linuxhost @ ubuntu / home 08:48 # 46] $ ls

[linuxhost @ ubuntu / home 08:51 # 47] $

\ W represent a complete working directory name, the more directories to write the name of the directory. But the main folder will be replaced with a ~.

\ A display time twenty-four hour format "HH: MM".

\ # Implementation of several commands.

\ $ Prompt, if you are root, the prompt is #, otherwise $.

Variable read keyboard, and an array declaration: read, array, declare

read read a variable from the keyboard input
[linuxhost @ ubuntu / home 09:12 # 54] $ read atest
This is a test
[linuxhost @ ubuntu / home 09:12 # 55] $ read -p "Please keyin your name:" -t 30 named
Please keyin your name: linuxhost
[linuxhost @ ubuntu / home 09:13 # 56] $ echo $ named
linuxhost

30 seconds without any operation, the command will be automatically skipped.

Type declare or typeset, variable declaration

If you use the latter declare not accept any arguments, bash will take the initiative to count out the variable name.
[linuxhost @ ubuntu / home 09:21 # 59] $ sum = 100 + 300 + 50
[linuxhost @ ubuntu / home 09:22 # 60] $ declare -i sum = 100 + 300 + 50
[linuxhost @ ubuntu / home 09:22 # 61] $ echo $ sum
450

Variable type defaults to a string, you can not develop variable type, 1 + 2 as a string instead of formula, so the execution of the first command will direct the output of 100 + 300 + 50 ..

The sum into environment variables:
[linuxhost @ ubuntu / home 09:13 # 57] $ declare -x sum
[linuxhost @ ubuntu / home 09:21 # 58] $ export | grep sum
declare -x sum

The sum into a read-only attribute can not be modified:
[linuxhost @ ubuntu / home 09:25 # 62] $ declare -r sum
[linuxhost @ ubuntu / home 09:29 # 63] $ sum = testgting
bash: sum: readonly variable

Let the sum become non-environmental variables Custom variables:
[linuxhost @ ubuntu / home 09:29 # 64] $ declare + x sum
[linuxhost @ ubuntu / home 09:30 # 65] $ declare -p sum
declare -ir sum = "450"

Array variable type
[linuxhost @ ubuntu / home 09:30 # 66] $ var [1] = "small min"
[linuxhost @ ubuntu / home 09:32 # 67] $ var [2] = "big min"
[linuxhost @ ubuntu / home 09:32 # 68] $ var [3] = "nice min"
[linuxhost @ ubuntu / home 09:32 # 69] $ echo "$ {var [1]}, $ {var [2]}, $ {var [3]}"
small min, big min, nice min

Limit relations with the file system and procedures ulimit

List your current status of all the limitations of data values:
[linuxhost @ ubuntu / home 09:33 # 70] $ ulimit -a

Limit user can only build the capacity of about 10MB file:
[linuxhost @ ubuntu / home 09:38 # 74] $ ulimit -f 10240
[linuxhost @ ubuntu / home 09:38 # 75] $ ulimit -a

Try to create a 20M file:
[linuxhost @ ubuntu ~ / Desktop / linux_study 09:41 # 81] $ sudo dd if = ~ / Desktop / linux_study / of = 123 bs = 1M count = 20

 [linuxhost @ ubuntu ~ / Desktop / linux_study 09:41 # 82] $ ll
 total 8
 drwxrwxr-x 2 linuxhost linuxhost 4096 Oct 30 09:41 ./
 drwxr-xr-x 3 linuxhost linuxhost 4096 Oct 30 07:43 ../
 -rw-r - r-- 1 root root 0 Oct 30 09:41 123

Delete the contents of variables, and alternative replacement

Remove and replace the contents of variables

Both the local directory deleted:
[linuxhost @ ubuntu ~ / Desktop / linux_study 09:47 # 85] $ echo $ path
/ Usr / local / sbin: / usr / local / bin: / usr / sbin: / usr / bin: / sbin: / bin: / usr / games: / usr / local / games
  [linuxhost @ ubuntu ~ / Desktop / linux_study 09:49 # 89] $ echo $ {path # / * local / bin:}
  / Usr / sbin: / usr / bin: / sbin: / bin: / usr / games: / usr / local / games

The sbin replaced SBIN:
linuxhost @ ubuntu ~ / Desktop / linux_study 13:06 # 95] $ echo $ {path / sbin / SBIN}
/ Usr / local / SBIN: / usr / local / bin: / usr / sbin: / usr / bin: / sbin: / bin: / usr / games: / usr / local / games
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:06 # 96] $ echo $ {path // sbin / SBIN}
/ Usr / local / SBIN: / usr / local / bin: / usr / SBIN: / usr / bin: / SBIN: / bin: / usr / games: / usr / local / games

Test and replace the contents of the variables:
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:07 # 97] $ echo $ username
  # Username empty string may not exist or is
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:09 # 98] $ username = $ {username-root}
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:10 # 99] $ echo $ username
root # is not provided, so take the initiative to give content named root
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:10 # 100] $ username = "linuxhost"
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:10 # 101] $ username = $ {username-root}
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:10 # 102] $ echo $ username
linuxhost


username-root variable have the option to be tested; content username- root variable, in this example, this section is not provided in the given variable content. Of course username: -root is in the variable is empty or not set, you will be able to replace the contents of the back.

If the old variable does not exist, we have to replace the contents of the new variable; but if the old variable exists, the new variable content to replace the old variable.
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:10 # 103] $ unset str; var = $ {str-newvar}
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:18 # 104] $ echo var = "$ var", str = "$ str"
var = newvar, str = # since str does not exist, so var is newvar
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:19 # 105] $ str = "oldvar"; var = $ {str-newvar}
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:20 # 106] $ echo var = "$ var", str = "$ str"
var = oldvar, str = oldvar # because str exist, so var equal to the content of str

About minus variables will not affect the old, if you want the old contents of the variables are replaced together, then you need to use the = sign:
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:20 # 107] $ unset str; var = $ {str = newvar}
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:25 # 108] $ echo var = "$ var", str = "$ str"
var = newvar, str = newvar
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:25 # 111] $ str = "oldvar"; var = $ {str = newvar}
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:25 # 112] $ echo var = "$ var", str = "$ str"
var = oldvar, str = oldvar

Command aliases and command history

Command aliases and set the alias unalias

Command aliases can make common but particularly long command modified to relatively simple commands, as well as adding some of the default options in the usual command above, you can prevent some accidentally deleted files accidentally happen. For example, the query hidden files, and takes a long look at page after page lists, we need to execute "ls -l | more", you can use the following command to be reduced to lm:
[linuxhost @ ubuntu ~ 13:35 # 120] $ alias lm = "ls -l | more"

In addition, aliasing settings can also replace the existing command, for example, when logged in as root, use the rm command to delete all the files, so be especially careful, rm provide a parameter to let us confirm whether to delete the file -i:
[linuxhost @ ubuntu ~ 13:35 # 123] $ alias rm = "rm -i"
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:39 # 127] $ rm 123
rm: remove write-protected regular empty file '123' y?

Just remove the named alias:
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:39 # 128] $ unalias rm

History command history

Once the query commands executed recently 4:
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:42 # 131] $ history 4

Bash Shell operating environment

Find the path and command sequence

Set an alias for the echo echo-n, and then view the echo sequence executed:
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:45 # 132] $ alias echo = "echo -n"
[linuxhost @ ubuntu ~ / Desktop / linux_study 13:47 # 134] $ type -a echo
echo is aliased to `echo -n '
echo is a shell builtin
echo is / bin / echo

bash login and welcome message: / etc / issue, / etc / motd

Terminal environment settings: stty.set

To view some of the current key content? You can use stty (setting tty terminal), stty can help set up the terminal input button on behalf of significance. We can use the stty-a to list the current environment, a list of all the keys.
linuxhost @ ubuntu: ~ $ stty -a
speed 38400 baud; rows 37; columns 80; line = 0;
intr = ^ C; quit = ^ \; erase = ^ ?; kill = ^ U; eof = ^ D; eol = M- ^ ?; eol2 = M- ^ ?;
swtch = M- ^ ?; start = ^ Q; stop = ^ S; susp = ^ Z; rprnt = ^ R; werase = ^ W;
lnext = ^ V; flush = ^ O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc ixany imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke

If you want stty related characters deleted:
linuxhost @ ubuntu: ~ $ stty erase ^ h

In addition to the stty, in fact, we also own some bash terminal settings set.
     
         
         
         
  More:      
 
- Running the open-source Swift under Linux platform (Linux)
- Basic Operation Linux files and directories (Linux)
- installation of Piwik under Ubuntu (Programming)
- PostgreSQL 9.4.3 Installation and Configuration under CentOS 6.5 (Database)
- A summary of Java multi-threaded programming - acquaintance multithreading (Programming)
- AngularJS achieve picture upload feature (Programming)
- CentOS 6.4 under PXE + Kickstart unattended operating system installation (Programming)
- MySQL can not write the data keyword conflicts (Database)
- SecureCRT use the configuration detailed tutorial (Linux)
- Linux /var/spool/ insufficient clientmqueue space solutions (Linux)
- Linux Network Programming - non-blocking program (Programming)
- CentOS6.0 successful installation and configuration OpenCV (Linux)
- Binding unofficial Google Drive and Ubuntu 14.04 LTS (Linux)
- LinkedList Basic Usage (Programming)
- CentOS 7.0 local address and configure yum source address priority (Linux)
- Netfilter / Iptables Comments (Linux)
- Varnish achieve page jump (Server)
- Sturdy build secure Linux server (Linux)
- Ruby and Python simple comparison (Programming)
- Empty password Linux operating system (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.