guo20082200的个人博客分享 http://blog.sciencenet.cn/u/guo20082200

博文

linux_centos第15次课 shell基础知识 逻辑判断

已有 1889 次阅读 2015-8-24 08:37 |个人分类:Linux学习|系统分类:科研笔记|关键词:学者| style, 基础知识

15次课 shell基础知识

 

复习上节课

[root@ghqpub]# vim test.sh

[root@ghq pub]# sh test.sh (执行脚本)

SatAug 22 07:04:31 PDT 2015

1234567890

[root@ghq pub]# chmod +x test.sh(加x权限)

[root@ghqpub]# du -sh

68K

[root@ghq pub]# ./test.sh (执行脚本)

SatAug 22 07:05:39 PDT 2015

1234567890

 

命令date的用法,+%F T Y y m d w W H M S s

[root@ghqpub]# date -d "+1 day"

SunAug 23 07:11:10 PDT 2015

[root@ghqpub]# date -d "-1 day"

FriAug 21 07:11:21 PDT 2015

[root@ghqpub]# date -d "-1 week"

SatAug 15 07:11:27 PDT 2015

[root@ghqpub]# date -d "-1 min"

SatAug 22 07:10:32 PDT 2015

[root@ghqpub]# date -d "-1 hour"

SatAug 22 06:11:37 PDT 2015

[root@ghqpub]# date -d "-1 month"

WedJul 22 07:11:46 PDT 2015

[root@ghqpub]# date -d "-1 year"

FriAug 22 07:11:57 PDT 2014

Shell变量,大小写字母,下划线和数字

 

逻辑判断

语法: if …………;then …………; fi

[root@ghqpub]# vim if1.sh

[root@ghqpub]# sh if1.sh

a> 1

查看执行过程(加-x

[root@ghqpub]# sh -x if1.sh

+a=10

+'[' 10 -gt 2 ']'

+echo 'a > 1'

a> 1

 

语法: if …………;then …………;else…………..;fi

语法: if  [-gt –ge –lt –le –eq -ne];  if [ $a –gt 10 || $a –lt 4]; if [ $a –gt 10 -o $a –lt 4] (-or);if [ $a –gt 10 -a $a –lt 4] (and)

[root@ghq pub]# cat if1.sh

#!/bin/bash

#if shell script.

#2015/08/22

 

a=10

if[ $a -lt 2 ]; #great than

then

   echo "a < 1"

elif[ $a -gt 100 ];

then

   echo "a > 100"

else

   echo "a !<1 && a!>100"

fi

[root@ghqpub]# if [ $a -gt 2 -o $a -lt 5 ]; then echo ok; fi

Ok

[root@ghq pub]# if [ -f 1.sh ]; then echo "1.sh is afile"; fi

判断1.sh是不是一个文件??

1.shis a file

[root@ghqpub]# if [ -d guo ]; then echo "guo is a dirctory"; fi

guois a directory

另外还有–e(存在exist -r(可读) -x(可执行)  -w(可写)(用的不多)

另一个逻辑判断: case

[root@ghqpub]# sh -x case.sh

+n=98

+a=0

+case $a in

+echo even

Even

 

[root@ghq pub]# bc

bc 1.06.95

Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free SoftwareFoundation, Inc.

This is free software with ABSOLUTELY NO WARRANTY.

For details type `warranty'.

 

循环 for while

[root@ghqpub]# seq 1 10

1

2

3

4

5

6

7

8

9

10

为啥会这样??

 

[root@ghqpub]# sh for.sh

1.0

1.1

1.2

1.3

1.4

1.5

1.6

1.7

1.8

1.9

2.0

[root@ghqpub]# cat for.sh

#!/bin/bash

#for shell script

#2015-08-23 V1.

 

for i in `seq 1 0.1 2` ` 不是单引号,是esc按键下面的按个按键。)

do

  echo $i

done

15.docx 

 

 

 




https://m.sciencenet.cn/blog-575910-915461.html

上一篇:linux_centos第24_2次课Apache的安装
下一篇:linux_centos第16次课 循环

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-26 07:06

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部