if的几种用法

文件及目录的判断

-F 当它是一个文件且存在时,这个就成立。

[[email protected] shell]# if [ -f 1.txt ]; then echo ok; fi
[[email protected] shell]# touch 1.txt
[[email protected] shell]# if [ -f 1.txt ]; then echo ok; fi
ok

-d 判断他是否是一个目录且存在

[[email protected] shell]# if [ -d /root/ ]; then echo ok; fi
ok

-r w x 判断一个文件的权限

[[email protected] shell]# if [ -r /root/ ]; then echo ok; fi  是否可读
ok
[[email protected] shell]# if [ -w /root/ ]; then echo ok; fi  是否可写
ok
[[email protected] shell]# if [ -x /root/ ]; then echo ok; fi  是否可执行
ok

-n 表示用来判断这个变量是否不为空

[[email protected] shell]# vim if2.sh
#!/bin/bash
read -p "olease input a number: " n
m=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$m" ]
then

  echo "the character you input is not a number ,please retry. "
else
  echo $n
fi
#!/bin/bash
read -p "olease input a number: " n
m=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$m" ]
then

  echo "the character you input is not a number ,please retry. "
else
  echo $n
fi

[[email protected] shell]# sh if2.sh  运行
olease input a number: dasdasdja   输入字母
the character you input is not a number ,please retry.   这不是数字 重新输入
[[email protected] shell]# sh if2.sh
olease input a number: 323  输入数字 直接打印
323

查看执行过程

[[email protected] shell]# sh -x if2.sh
+ read -p ‘olease input a number: ‘ n
olease input a number: a
++ sed ‘s/[0-9]//g‘
++ echo a
+ m=a
+ ‘[‘ -n a ‘]‘
+ echo ‘the character you input is not a number ,please retry. ‘
the character you input is not a number ,please retry.

[[email protected] shell]# sh -x if2.sh
+ read -p ‘olease input a number: ‘ n
olease input a number: 2
++ sed ‘s/[0-9]//g‘
++ echo 2
+ m=
+ ‘[‘ -n ‘‘ ‘]‘
+ echo 2
2

-z 表示这个变量是否为空

[[email protected] shell]# vim if2.sh
#!/bin/bash
read -p "olease input a number: " n
m=`echo $n|sed ‘s/[0-9]//g‘`
if [ -z "$m" ]
then

  echo "the character you input is not a number ,please retry. "
else
  echo $n
fi
[[email protected] shell]# sh if2.sh
olease input a number: 21323
the character you input is not a number ,please retry.
[[email protected] shell]# sh if2.sh
olease input a number: dsadasdw
dsadasdw

查看执行过程

[[email protected] shell]# sh -x if2.sh
+ read -p ‘olease input a number: ‘ n
olease input a number: 1
++ sed ‘s/[0-9]//g‘
++ echo 1
+ m=
+ ‘[‘ -z ‘‘ ‘]‘
+ echo ‘the character you input is not a number ,please retry. ‘
the character you input is not a number ,please retry.
[[email protected] shell]# sh -x if2.sh
+ read -p ‘olease input a number: ‘ n
olease input a number: dsa
++ sed ‘s/[0-9]//g‘
++ echo dsa
+ m=dsa
+ ‘[‘ -z dsa ‘]‘
+ echo dsa
dsa

判断一个用户是否存在

[[email protected] shell]# if grep -q ‘^root:‘ /etc/passwd; then echo "root exist." ; fi
root exist.
[[email protected] shell]# if grep -q ‘^roota:‘ /etc/passwd; then echo "root exist." ; fi
时间: 2024-08-09 02:18:36

if的几种用法的相关文章

Linux内核中等待队列的几种用法

Linux内核里的等待队列机制在做驱动开发时用的非常多,多用来实现阻塞式访问,下面简单总结了等待队列的四种用法,希望对读者有所帮助. 1. 睡眠等待某个条件发生(条件为假时睡眠): 睡眠方式:wait_event, wait_event_interruptible 唤醒方式:wake_up (唤醒时要检测条件是否为真,如果还为假则继续睡眠,唤醒前一定要把条件变为真) 2. 手工休眠方式一: 1)建立并初始化一个等待队列项 DEFINE_WAIT(my_wait) <==> wait_queue

[java]static关键字的四种用法

在java的关键字中,static和final是两个我们必须掌握的关键字.不同于其他关键字,他们都有多种用法,而且在一定环境下使用,可以提高程序的运行性能,优化程序的结构.下面我们先来了解一下static关键字及其用法. static关键字 1.修饰成员变量 在我们平时的使用当中,static最常用的功能就是修饰类的属性和方法,让他们成为类的成员属性和方法,我们通常将用static修饰的成员称为类成员或者静态成员,这句话挺起来都点奇怪,其实这是相对于对象的属性和方法来说的.请看下面的例子:(未避

JSP中的include的两种用法

1.两种用法 <@inlcude file ="header.jsp"/> 此时引入的是静态的jsp文件,它将引入的jsp中的源代码原封不动地附加到当前文件中,所以在jsp程序中使用这个指令的时候file里面的值(即要导入的文件)不能带多余的标签或是与当前jsp文件重复的东西.例如里面不要包含<html><body>这样的标签,因为是把源代码原封不动的附加过来,所以会与当前的jsp中的这样的标签重复导致出错. <jsp:include page

Android---24---Spinner的两种用法

Spinner是一个列表选择框,它有两种用法: 一种是使用android:entries属性的,一种是不使用该属性,通过动态的添加Adapter来实现的. 第一种: MainActivity.java: import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import andr

js中继承的几种用法总结(apply,call,prototype)

本篇文章主要介绍了js中继承的几种用法总结(apply,call,prototype) 需要的朋友可以过来参考下,希望对大家有所帮助 一,js中对象继承 js中有三种继承方式 1.js原型(prototype)实现继承 <SPAN style="<SPAN style="FONT-SIZE: 18px"><html>   <body>  <script type="text/javascript"> 

指针与引用的区别以及引用的三种用法

1.指针与引用的区别: 指针是一块内存的地址值,而引用是一块内存的别名. 下面引自:http://www.cnblogs.com/kingln/articles/1129114.html 从概念上讲.指针从本质上讲就是存放变量地址的一个变量,在逻辑上是独立的,它可以被改变,包括其所指向的地址的改变和其指向的地址中所存放的数据的改变. 而引用是一个别名,它在逻辑上不是独立的,它的存在具有依附性,所以引用必须在一开始就被初始化,而且其引用的对象在其整个生命周期中是不能被改变的(自始至终只能依附于同一

poj 1511 Dijkstra的另一种用法---求其他点到源点的最短路

传送门:http://poj.org/problem?id=1511 题目其实到现在我都没读懂,到底是哪里看出来的,ans是源点到各个点最短路的和外加各个点到源点的最短路的和,不过重要的是学到dijkstra的另一种用法--求各个点到源点的距离,原理不难理解,就是把dijkstra求单源最短路径的过程逆过来: 1.取源点加入S集合,设其余点在T集合 2.找到达源点的最小边,将该边连的点加入S,更新T到S的各个点的最短路径,取最短的边,继续2的过程 而dijastra求单源最短路径的过程 1.取源

[java]final关键字的几种用法

在java的关键字中,static和final是两个我们必须掌握的关键字.不同于其他关键字,他们都有多种用法,而且在一定环境下使用,可以提高程序的运行性能,优化程序的结构.下面我们来了解一下final关键字及其用法. final关键字 在java中,final的含义在不同的场景下有细微的差别,但总体上来说,它指的是"这是不可变的".下面,我们来讲final的四种主要用法. 1.修饰数据 在编写程序时,我们经常需要说明一个数据是不可变的,我们成为常量.在java中,用final关键字修饰

operator 的两种用法

C++,有时它的确是个耐玩的东东,就比如operator,它有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换). 1.操作符重载C++可以通过operator实现重载操作符,格式如下:类型T operator 操作符 (),比如重载+,比如下面这个例子template<typename T> class A{public:     const T operator+(const T& rhs)     {  

C#中this的 四种 用法

C#中的this用法,相信大家应该有用过,但你用过几种?以下是个人总结的this几种用法,欢迎大家拍砖,废话少说,直接列出用法及相关代码. this用法1:限定被相似的名称隐藏的成员 /// <summary> /// /******************************************/ /// /* this用法1:限定被相似的名称隐藏的成员 */ /// /******************************************/ /// </summ