ProDeveloperTutorial.com

教程和编程解决方案
菜单
  • Shell脚本
  • 系统设计
  • Linux系统编程
  • 4g LTE
  • 编码问题
  • C
  • C ++
  • DSA
  • GIT

Shell脚本第10章:Shell脚本文件操作

前开发者教程 二月13,2019

在大多数情况下,shell脚本用于与文件进行交互。

档案范例1:

以下是检查文件是否存在的程序

在此示例中,我们从用户那里输入文件名。注意“\c”选项,用于将光标保持在同一行。然后我们用“-e”检查用户输入的文件是否存在的选项。这将检查同一目录中文件的存在。

#!/bin/bash

echo -e "Enter the file name \c"
read file_name

if [ -e $file_name ]
then
echo "File found"
else
echo "File not found"
fi

输出:

sh file.sh
Enter the file name hello.txt
File not found

 

由于最初该文件不存在,它将显示找不到文件。
我们将使用touch命令创建一个文件,然后再次运行该程序。

touch hello.txt

sh file.sh
Enter the file name hello.txt
File found

文件示例2:

“-f”选项用于检查文件是否存在以及是否为常规文件。

 

#!/bin/bash

echo -e "Enter the file name \c"
read file_name

if [ -f $file_name ]
then
echo "File found and is a regular file"
else
echo "File not found"
fi

输出:

sh file.sh
Enter the file name hello.txt
File found and is a regular file

 

档案范例3:

“-d”option用于检查文件是否存在,是否为目录文件。

#!/bin/bash

echo -e "Enter the file name \c"
read file_name

if [ -d $file_name ]
then
echo "File found and is a directory file"
else
echo "File not found"
fi

输出:

sh file.sh
Enter the file name mydir
File found and is a directory file

 

相似地
“-b”检查块特殊文件。
“-c”检查字符特殊文件。
“-s”检查文件是否为空。
“-r”TP检查文件是否具有读取权限
“-w”TP检查文件是否具有写权限
“-x”TP检查文件是否具有执行权限

文件示例4:

在这个例子中,我们将打开一个文件,检查我们是否具有写权限,然后在该文件中写入一些内容。写入文件使用“cat” command.

#!/bin/bash

echo -e "Enter the file name \c"
read file_name

if [ -f $file_name ]
then
echo "File found"
if [ -w $file_name ]
then
echo "File is having write permission. Write text below. Ctrl +
cat >> $file_name
fi
else
echo "File not found"
fi

输出:

sh file.sh
Enter the file name hello.txt
File found
File is having write permission. Write text below. Ctrl + D to exit
来自prodevelopertutorial.com的Hello World

现在的文字“来自prodevelopertutorial.com的Hello World” will be written in “hello.txt” file.

文件示例5:

在此示例中,我们将研究如何使用while循环读取文件:

使用“read” command.

使用read命令,我们将使用read命令和一个变量,然后回显该变量以输出。我们用“<”输入重定向以输入文件。

#!/bin/bash

echo "Reading the file hello.txt"

while 读 r
do
echo $r
done < hello.txt

输出:

sh 2_file.sh
Reading the file hello.txt
来自prodevelopertutorial.com的Hello World

使用“read” and pipe command.

在上一个示例中,我们使用了“<”命令。在此示例中,我们使用“|”输入文件名的命令。

#!/bin/bash

echo "Reading the file hello.txt"

cat hello.txt | while 读 r
do
echo $r
done

 

使用“IFS” and pipe command.

如果文件具有特殊字符,例如文件缩进或空格,制表符或换行符,则上述方法无法准备这些值。因此,在那个地方我们使用“IFS”关键词。 IFS代表内部字段分隔符。

#!/bin/bash

echo "Reading the file /etc/resolv.conf"

while IFS= 读 -r line
do
echo $line
done < /etc/resolv.conf

输出:

sh 2_file.sh
Reading the file /etc/resolv.conf
# Generated by dhcpcd
# /etc/resolv.conf.head can replace this line
# /etc/resolv.conf.tail can replace this line

 

 

该网站上可用的教程列表:

C编程20+章C ++编程80+章
100多个编码问题数据结构和算法85+章
系统设计20+章Shell脚本编写12章
4g LTE 60+章节最常见的编码问题
5G NR 50+章Linux系统编程20+章
分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

每天我们都会讨论竞争性编程问题,请加入我们的网站:   电报频道

ProDeveloperTutorial.com

教程和编程解决方案
版权© 2020 ProDeveloperTutorial.com
从以下课程获得热门课程: 教育性的

  • <menu id="Atu62Bf" class="AqccqEG"><sup id="T2HUY6l"></sup></menu>
    <menu class="eFoPyfM"></menu>







            • <s class="ML9a9Y3"></s>