linux shell 学习

Linux 常用命令

ls -F:目录会显示/

[root@milab ~]# ls -iFl
总用量 12
 69199780 -rw-------. 1 root root 1496 6月   8 2016 anaconda-ks.cfg
 77471938 drwxr-xr-x. 2 root root    6 9月  18 21:49 svntest/
 74325841 -rw-r--r--. 1 root root    7 5月  31 20:03 wer
  1455733 drwxr-xr-x. 2 root root    6 6月   8 2016 公共/
103304633 drwxr-xr-x. 2 root root    6 6月   8 2016 模板/
  1455734 drwxr-xr-x. 2 root root    6 6月   8 2016 视频/
103304634 drwxr-xr-x. 2 root root 4096 9月  30 2016 图片/
 35680426 drwxr-xr-x. 2 root root    6 6月   8 2016 文档/
 71066179 drwxr-xr-x. 2 root root    6 6月   8 2016 下载/
 71066180 drwxr-xr-x. 2 root root    6 6月   8 2016 音乐/
 35680425 drwxr-xr-x. 2 root root   41 9月   1 2016 桌面/

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

逐行查看 

set | more

shell 中单引号 打印所有字符,双引号 可打印变量

[root@milab ~]# set |grep HIST
HISTCONTROL=ignoredups
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000

PS1 定制命令提示符的样式

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]#

自定义变量只能在当前用户的当前shell中生效 想升级当前shell生效需要 export ,永久生效需修改profile

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

[root@milab 22:03:20 /]# ls -l /dev/std*
lrwxrwxrwx. 1 root root 15 10月 21 20:46 /dev/stderr -> /proc/self/fd/2    //2> 错误输出
lrwxrwxrwx. 1 root root 15 10月 21 20:46 /dev/stdin -> /proc/self/fd/0		//1> 正确输出
lrwxrwxrwx. 1 root root 15 10月 21 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: 未找到命令...
相似命令是: 'cc'
[root@milab 22:08:42 ~]# bbb 2>>err
[root@milab 22:08:54 ~]# cat err
bash: ccc: 未找到命令...
相似命令是: 'cc'
bash: bbb: 未找到命令...

[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’: 没有那个文件或目录
[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’: 没有那个文件或目录
[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’: 没有那个文件或目录

[root@milab 22:34:20 /bin]# expr length "12345678"
8
字符串匹配

[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: 语法错误
[root@milab 22:41:07 /bin]# expr substr "$str" 3
expr: 语法错误
[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: 语法错误
[root@milab 22:43:09 /bin]# expr $str :cdeff
expr: 语法错误
[root@milab 22:43:18 /bin]# expr $str : cdeff
0
[root@milab 22:43:20 /bin]# expr $str : '.*'// 单引号中正则表达式
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 $n1 < $n2
bash: 11: 没有那个文件或目录
[root@milab 22:49:43 /bin]# expr $n1 \< $n2
1
[root@milab 22:49:48 /bin]# expr $n1 \= $n2
0
[root@milab 22:49:53 /bin]# expr $n1 \= $n3
1
[root@milab 22:49:56 /bin]# expr $n1 == $n3
1
[root@milab 22:50:00 /bin]# expr $n1 == 10
1
[root@milab 22:50:47 /bin]# re=$[ $n1 == 10]
[root@milab 22:51:21 /bin]# echo $re
1
[root@milab 22:51:27 /bin]# re=$[ $n1 = 10]
bash: 10 = 10: 尝试给非变量赋值 (错误符号是 "= 10")
[root@milab 22:51:37 /bin]# 

bc命令(实现浮点数计算)
[root@milab 22:52:59 /bin]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
1*2
2
1.1*5.55555
6.11110
 
 
 
100/3
33
scale=5
100/3
33.33333
quit
[root@milab 23:03:03 /bin]# resule=`bc<<EOF
scale=4
a1 = $v1 * $v2
b1 = $v3 * $v1
a1+b1
EOF
`
[root@milab 23:03:27 /bin]# echo $bc
 
[root@milab 23:03:38 /bin]# echo $resule
6.16
函数使用
function fun1()
{
        echo "function1"
 
}
fun2(){
        echo "function2"
}
fun1
fun2
函数返回值

echo "$?"  0 为正确执行,否则出错
函数退出状态码  

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


函数参数的处理:  $# 参数个数; $1  第一个参数  $2 第二个参数
[root@milab 23:23:07 ~]# cat test2.sh 
#!/bin/bash
 
add()
{
        if [ $# -eq 2 ];then
                result=$[ $1 + $2]
                echo $result
        else
                echo "please input 2 parame"
                return 1
        fi
}
 
value=`add $1 $2`
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
[root@milab 23:23:31 ~]# 

引用函数库

source  文件路径  或者  . 文件路径  或者在脚本中加入。

shell 的风格,用倒序的字母单词和 正序的单词配对。
比如 if 语句, 结束时用 fi 来配对
esac是和case配对的,是多路分支的语句







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值