Linux Shell Learning __linux

Source: Internet
Author: User

Linux Common Commands

LS-F: Catalog will show/

[Root@milab ~]# ls-ifl
total dosage
 69199780-rw-------. 1 root root 1496 June   8 2016 anaconda-ks.cfg
 77471938 Dr Wxr-xr-x. 2 root    6 September  21:49 svntest/
 74325841-rw-r--r--. 1 root root    7 May to  20:03 wer
  1455733 drwx R-xr-x. 2 root    6 June   8 2016 public/
103304633 drwxr-xr-x 2 root root    6 June   8 2016 Templates/
  1455734 drwxr- Xr-x. 2 root    6 June   8 2016 video/
103304634 drwxr-xr-x. 2 root root 4096 September  30 2016 images/
 35680426 drwxr- Xr-x. 2 root    6 June   8 2016 Documents/
 71066179 drwxr-xr-x. 2 root root    6 June   8 2016 download/
 71066180 drwxr- Xr-x. 2 root    6 June   8 2016 music/
 35680425 drwxr-xr-x. 2 root root   41 September   1 2016 Desktop/

[Root@milab ~]# cat test.sh 
#/bin/bash
echo "input A:"
read a
 
echo "Input B:"
read B
 
result=$[$a + $b  ]
echo "Result:" $result

View by line

Set | More

Single quotation marks in the shell print all characters, double quotes to print variables

[Root@milab ~]# set |grep HIST
histcontrol=ignoredups histfile=/root/.bash_history histfilesize=1000
histsize=1000

PS1 custom command Prompt style

Ps1=[root@milab ~ 21:23:12]#  ps1= ' [\u@\h \w \t]\$ ' Ps1=[root@milab
~ 21:23:23]# CD/
ps1=[root@milab/21:23 : 28]# cd/usr/src/
ps1=[root@milab/usr/src 21:23:38]#

The custom variable can only take effect in the current user's shell want to upgrade the current shell to take effect need to export, permanent entry needs to modify profile

echo  "ps1=" [\u@\h \ t \w]\$ ' ">>/etc/profile
source/etc/profile

[Root@milab 22:03:20/]# ls-l/dev/std* lrwxrwxrwx
. 1 root 15 October 20:46/dev/stderr-> >//2> Error Output
lrwxrwxrwx 1 root 15 October 20:46/dev/stdin->/proc/self/fd/0//1>		correct output lrwxrwxrwx
. 1 root 15 October 20:46/dev/stdout->/PROC/SELF/FD/1

[Root@milab 22:08:22 ~]# CCC 2>err  //
[Root@milab 22:08:38 ~]# Cat err 
BASH:CCC: Command not found ...
Similar commands are: ' CC '
[root@milab 22:08:42 ~]# bbb 2>>err
[Root@milab 22:08:54 ~]# Cat err
BASH:CCC: Command not found ... c12/> similar command is: ' CC '
bash:bbb: no command found ...

[Root@milab 22:10:49 ~]# ls test.sh 1>suc
[Root@milab 22:10:54 ~]# cat suc 
test.sh

[Root@milab 22:14:41/usr/src]# find/usr/src/123 1>suc 2>err
[Root@milab 22:15:08/usr/src]# cat suc/
usr /src/
/usr/src/debug
/usr/src/kernels
/usr/src/suc
/usr/src/err
[Root@milab 22:15:11/usr/ src]# Cat Err Find 
: ' 123 ': Not that file or directory
[Root@milab 22:15:17/usr/src]# find/usr/src/123 >all 2>&1
[ Root@milab 22:15:47/usr/src]# Cat All 
/usr/src/
/usr/src/debug
/usr/src/kernels
/usr/src/suc
/usr/src/err
/usr/src/all find
: ' 123 ': No file or directory
[Root@milab 22:15:50/usr/src]# find/usr/src/ 123 &>all1
[Root@milab 22:16:42/usr/src]# cat all1
/usr/src/
/usr/src/debug
/usr/src/ Kernels
/usr/src/suc
/usr/src/err
/usr/src/all
/usr/src/all1 find
: ' 123 ': no file or directory

[Root@milab 22:34:20/bin]# expr Length "12345678"
8
String matching

[Root@milab 22:37:51/bin]# str= "abcdefffff"
[Root@milab 22:40:36/bin]# echo $sr
 
[Root@milab 22:40:44/bin]# echo $str
abcdefffff
[root@milab 22:40:47/bin]# expr substr "$str" de
expr: syntax error
[Root@milab 22:41:07/  bin]# expr substr "$str" 3
expr: syntax error
[root@milab 22:41:25/bin]# expr substr "$str" 3 5
Cdeff
[Root@milab 22:41:36/bin]# str= "Abcdefffff"
[root@milab 22:41:42/bin]# expr substr "$str" 3 5
Cdeff
[Root@milab 22: 41:44/bin]# expr $str: Cdeff
expr: syntax error
[Root@milab 22:43:09/bin]# expr $str: Cdeff
expr: syntax error
[roo T@milab 22:43:18/bin]# expr $str: Cdeff
0
[root@milab 22:43:20/bin]# expr $str: '. * '//single quotation mark positive expression
10

[Root@milab 22:47:23/bin]# n1=10
[Root@milab 22:49:19/bin]# n2=11
[Root@milab 22:49:24/bin]# n3=10]
[ Root@milab 22:49:27/bin]# Expr $n 1 < $n 2
bash:11: No file or directory
[root@milab 22:49:43/bin]# expr $n 1 \< $n 2
  1
[Root@milab 22:49:48/bin]# expr $n 1 \= $n 2
0
[root@milab 22:49:53/bin]# expr $n 1 \= $n 3
1
[root @milab 22:49:56/bin]# expr $n 1 = $n 3
1
[Root@milab 22:50:00/bin]# expr $n 1 =
1
[Root@milab 22:5  0:47/bin]# re=$[$n 1 =
[Root@milab 22:51:21/bin]# echo $re
1
[Root@milab 22:51:27/bin]# re=$[$n 1 =
bash:10 = 10: Trying to assign a value to a non variable (the error symbol is "= 10")

BC Command (realize floating-point count)
[Root@milab 22:52:59/bin]# BC
BC 1.06.95
Copyright 1991-1994, 1997, 1998, Watts, 2006 Software Free Founda tion, Inc.
This are free software with absolutely NO WARRANTY.
For details type ' warranty '. 
1*2
2
1.1*5.55555
6.11110
 
 
 
100/3
-scale=5 100/3 33.33333 quit
[Root@milab 23:03:03/bin]# resule= ' bc<<eof
scale=4 a1
= $v 1 * $v 2
b1 = $v 3 * $v 1
a1+b1
eof
  
    '
[Root@milab 23:03:27/bin]# echo $BC
 
[Root@milab 23:03:38/bin]# echo $resule
6.16
  
function uses
function fun1 ()
{
        echo "function1"
 
}
fun2 () {
        echo "function2"
}
fun1
fun2
function return value

echo "$?" 0 for correct execution, otherwise error
function Exit Status Code

[Root@milab 23:15:32 ~]# cat test.sh 
#/bin/bash
function fun1
{
        echo "function1" return
        10
}
Fun2 () {
        echo "function2" return
}
fun1
echo "$?"
 
Fun2
echo "$?"
Value= ' fun2 '
echo $value   #输出fun2的文本  function2
[Root@milab 23:15:41 ~]#./test.sh 
Function1
function2
function2


The processing of function parameters: the number of $# parameters; $ first Argument $ second argument

[Root@milab 23:23:07 ~]# cat test2.sh 
#!/bin/bash
 
Add ()
{
        if [$#-eq 2];then
                result=$[$ + $]
                echo $result
        Else
                echo "Please input 2 Parame" return
                1
        fi
}
 
value= ' add $ '
if [$?-eq 0];then
        Echo $value
else 
        echo "ERR: $value"
fi
[Root@milab 23:23:23 ~]#./test2.sh 1
Err:please input 2 parame
[Root@milab 23:23:29 ~]#./test2.sh 1 2
3

referencing function libraries

source file path or. File path or join in the script.

The shell style, paired with a reversed letter word and a positive sequence of words.
Like if statement, match with Fi at the end
ESAC is a multiple-branch statement that matches the case.







Related Article

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.