shell中的脚本程序(一)

本文介绍了如何使用Shell脚本进行一系列操作,包括判断文件类型、改变字体颜色和背景颜色、修改HTTP服务端口、使用for循环、根据文件内容创建和修改用户,以及移动和重命名配置文件。详细讲解了每个步骤的实现方法和注意事项。
摘要由CSDN通过智能技术生成

1.使用shell脚本判断文件的类型:

当没有给文件时,输出“Please show me a file after script",当文件不存在时,输出"文件名称 is not exist",当文件的类型是其他类型时,输出每个文件的文件类型名称
<1>建立一个shell脚本(幻数是sh),查看/mnt目录下的文件

[root@localhost mnt]# vim check_file.sh
[root@localhost mnt]# ll

在这里插入图片描述
shell脚本中写入的内容是:

#!/bin/bash
[ -z "$1" ] && {
        echo "Error: Please show me a file after script"
        exit
}
[ -e "$1" ] || {
        echo "$1 is not exist"
        exit
}
[ -L "$1" ] && {
        echo "$1 is a link file"
        exit
}
[ -f "$1" ] && {
        echo "$1 is a common file"
        exit
}
[ -d "$1" ] && {
        echo "$1 is a directory"
        exit
}
[ -c "$1" ] && {
        echo "$1 is a char file"
        exit
}
[ -b "$1" ] && {
        echo "$1 is a block file"
        exit
}
[ -S "$1" ] && {
        echo "$1 is a socket file"
        exit
}

在这里插入图片描述
<2>测试:判断文件的类型

[root@localhost mnt]# sh check_file.sh	##当没有输入任何文件时
[root@localhost mnt]# sh check_file.sh file1	##当输入的文件是链接文件时(file1文件也是文本文件,但是因为在脚本中判断是链接文件比判断是文本文件更前,所以只显示是链接文件)
[root@localhost mnt]# sh check_file.sh /mnt	##当输入的是一个目录时
[root@localhost mnt]# sh check_file.sh file2	##当输入的文件不存在时
[root@localhost mnt]# sh check_file.sh /dev/vda	##当输入的文件是一个块设备文件时
[root@localhost mnt]# sh check_fi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值