编写shell脚本,接收用户从键盘上输入的文件名,然后判断在当前目录下该文件名是否存在,不存在时提示“the file is not exists”
时间: 2023-12-07 12:03:38 浏览: 48
可以使用以下shell脚本实现:
```bash
#!/bin/bash
echo "Please enter the file name: "
read filename
if [ -e "$filename" ]; then
echo "The file exists."
else
echo "The file is not exists."
fi
```
首先使用 `read` 命令接收用户输入的文件名,然后使用 `if` 判断当前目录下是否存在该文件,如果存在就输出“the file exists”,否则输出“the file is not exists”。其中 `-e` 参数用于判断文件是否存在。
相关问题
编写shell脚本,接受用户从键盘上输入的文件名,然后判断在当前目录下该文件名是否存在
可以使用以下脚本实现:
```bash
#!/bin/bash
# 从键盘读入文件名
read -p "请输入文件名: " filename
# 判断文件是否存在
if [ -e "$filename" ]; then
echo "$filename 存在"
else
echo "$filename 不存在"
fi
```
解释:
- `read -p` 用于从键盘读入用户输入的文件名,并将其保存在变量 `filename` 中。
- `-e` 参数用于测试文件是否存在,如果文件存在,则输出提示信息 `"文件名 存在"`,否则输出 `"文件名 不存在"`。
编写Shell脚本,接收用户从键盘上输入的文件名,然后判断在当前目录下该文件名是否存在,不存在时提示"the file is not exists",如果存在输出"the file is exists"。
可以使用以下Shell脚本实现:
```shell
#!/bin/bash
# 从用户输入中获取文件名
echo "Please input the file name:"
read filename
# 判断文件是否存在
if [ -e "$filename" ]
then
echo "the file is exists"
else
echo "the file is not exists"
fi
```
运行脚本后,会提示用户输入文件名,然后根据文件是否存在输出对应的提示信息。注意,`-e`参数表示判断文件是否存在,还有其他参数可以用来判断文件类型等,可以根据需求进行修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)