简体   繁体   中英

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. Please what have I done wrong?

to run the script and pass in two arguments from 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.

you are not currently derefencing the file1 variable In the line

if [ -e file1 ]

You need to add a $ to derefence the variable and access its content as so

if [ -e $file1 ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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