Linux shell脚本 判断用户输入的文件类型


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55


编写一个脚本,从键盘输入一个文件,判断它是否存在,如果存在就判断它是什么类型的文件;并用对应的颜色输出

脚本如下:

#!/bin/bash

#function:test file type

#by:ZYJTF 孤云暮雨

#blog:zhangdaifu.blog.51cto.com

read -t 60 -p "请输入一个文件:" filename

if [ -z $filename ]

then

    echo -e "\033[41;5m 错误,请输入文件! \033[0m"

    exit 222

fi

if [ ! -e $filename ]

then

    echo "你输入的文件不存在"

fi

if [ -f $filename ]

then

    echo "你输入的文件存在;并且是一个普通文件"

fi

if [ -d $filename ]

then

    echo -e "\033[34;1m 你输入的文件存在;并且是一个目录 \033[0m"

fi

if [ -L $filename ]

then

    echo -e "\033[36m 你输入的文件存在;并且是一个软链接文件 \033[0m"

fi

if [ -b $filename ]

then

    echo -e "\033[33m 你输入的文件存在;并且是一个块设备文件 \033[0m"

fi

if [ -c $filename ]

then

    echo -e "\033[33m 你输入的文件存在;并且是一个字符设备文件 \033[0m"

fi

if [ -p $filename ]

then

    echo -e "\033[33m 你输入的文件存在;并且是一个管道文件 \033[0m"

fi

if [ -S $filename ]

then

    echo -e "\033[35m 你输入的文件存在;并且是一个套接字文件 \033[0m"

fi

时间: 2024-07-31 00:54:48

Linux shell脚本 判断用户输入的文件类型的相关文章

【shell】Linux shell 之 判断用户输入的变量是否为数字

本文内容:判断用户输入的参数是否为数字 在shell中如何进行计算? 方式一 [[email protected] scripts]# echo $((1+2)) 3 方式二 [[email protected] scripts]# expr 2 + 3 5 [[email protected] scripts]# 注意:使用方式二的时候,要求必须要有间隔.如果使用的是乘法,号必须进行转义写为 \ [[email protected] scripts]# expr 2 * 3 expr: 语法错

Linux shell脚本判断服务器网络是否可以上网

Linux shell脚本判断网络畅通 介绍 在编写shell脚本时,有的功能需要确保服务器网络是可以上网才可以往下执行,那么此时就需要有个函数来判断服务器网络状态 我们可以通过curl来访问 www.baidu.com,从而判断服务器网络状态是否可以畅通的 网络状态判断 #!/bin/bash #检测网络链接畅通 function network() { #超时时间 local timeout=1 #目标网站 local target=www.baidu.com #获取响应状态码 local

Linux Shell脚本去掉几类常见文件中的注释

Linux操作系统中去掉各类文件中的注释这个功能比较常用,通常用在查看一个较长的文件,又不想看注释的情况.通常这些文件包括C语言编写的*.c.*.h文件.cpp文件.*.xml文件.*.sh shell脚本文件.*.ini *.conf配置文件.*.php *.py *.pl等编程语言编写的文件以及无扩展名的一些可执行文件等. 实现这个功能并不复杂,通常注释风格就那么几种,在编写脚本过程中只需要编写出合适的正则表达式以及运用适当的文本处理工具(grep.sed等)即可. 针对几种常见的注释风格编

Linux Shell脚本逐行读取多个文件

现有file1.file2.file3三个文件,其内容如下 $cat file1 f1_1 f1_2 f1_3 $cat file2 f2_1 f2_2 f2_3 $cat file3 f3_1 f3_2 f3_3 编写shell脚本逐行读取这三个文件 #!/bin/bash cat file1 file2 file3 |while read p   do     echo $p   done [思考] 采用done引入多个文件,怎么实现? #!/bin/bashwhile read p  do

shell脚本读取用户输入并与之交互

举个栗子: #! /bin/bash echo -e "你真要想要执行此操作吗,yes or no?" read Confirm case $Confirm in y|Y|yes|Yes) echo "做你想做的事情吧" ;; *) echo "操作终止了!" esac

shell脚本判断是否存在某文件

#!/bin/sh for i in `seq 1 3`do pidfile_="worldd${i}.pid" if [ -f ${pidfile_} ]; then pid_=`cat ${pidfile_}` rm -f ${pidfile_} echo -n "killing worldd ${i}, pid ${pid_} .." kill -9 ${pid_} fidone echo "Done."

linux shell 脚本获取和替换文件中特定内容

1.从一串字符串中获取特定的信息 要求1:获取本机IP:menu.lst为系统镜象的IP配置文件,需要从中获取到本机IP信息(从文件获取信息) 1 timeout 1 2 default 0 3 4 title live 5 find --set-root /casper/vmlinuz 6 kernel /casper/vmlinuz boot=casper ignore_uuid showmounts ip=eth0,10.0.66.66,255.255.240.0,10.0.64.3 7

《Linux Shell脚本攻略》 笔记 第三章:文件操作

<Linux Shell脚本攻略> 笔记 第三章:文件操作 1.生产任意大小的文件 [[email protected] dd_test]# [[email protected] dd_test]# dd if=/dev/zero of=junk.data bs=1k count=10 10+0 records in 10+0 records out 10240 bytes (10 kB) copied, 0.00137023 s, 7.5 MB/s 2.文件系统相关测试 [ -f $file

Linux Shell脚本 几种循环语句创建用户的方法

大家好,我是孤云暮雨,今天给大家带来的是"Linux Shell脚本 几种循环语句创建用户的方法" 添加user1-user20用户 for循环: #!/bin/bash for i in {1..20} do useradd user$i echo "user$i Users to add success" done for循环(C风格): #!/bin/bash for ((i=1;i<=20;i++)) do useradd user$i &&a