简体   繁体   English

Bash shell 脚本文件存在

[英]Bash shell script file exists

Hi my bash script says the that the first parameter passed into it doesn't exist as a file but it does.嗨,我的 bash 脚本说传递给它的第一个参数不作为文件存在,但确实存在。 Please what have I done wrong?请问我做错了什么?

to run the script and pass in two arguments from linux: ./concantenatefile.sh file1.txt file2.txt运行脚本并从 linux 传入两个参数: ./concantenatefile.sh file1.txt file2.txt

#!/bin/bash

file1="$1"

if [ -e file1 ]
then    
    echo "The file $file1 exists"
else    
    echo "The file $file1 doesn't exist"
fi

It says The file file1.txt doesn't exist However there is a file called file1.txt in the present working directory.它说The file file1.txt doesn't exist但是在当前工作目录中有一个名为file1.txt的文件。

you are not currently derefencing the file1 variable In the line您当前没有取消引用 file1 变量在行中

if [ -e file1 ]

You need to add a $ to derefence the variable and access its content as so您需要添加一个 $ 来取消引用变量并访问其内容

if [ -e $file1 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM