2016-05-31 15 views
1

Ich bin neu in Unix Shell Scripting.Datei anzeigen, wenn sie im angegebenen Pfad vorhanden ist

Ich habe eine Datei erstellt, die einen Parameter als einen Dateinamen nimmt, um in dem angegebenen Pfad in der Datei herauszufinden. Wenn die Datei gefunden wurde, sollte sie die Datei anzeigen oder die entsprechende Nachricht anzeigen.

Datei: FilePath.sh

#!/bin/sh 

Given_Path=/user/document/workplace/day1 

File_Name=$Given_Path/$1 

# Here i got stuck to write the if condition, 
# to check for the file is present in the given 
# path or not. 
if [---------] #Unable to write condition for this problem. 
then 
    echo $1 
else 
    echo "File not found" 
fi 

Run: Laufende Datei

$ bash FilePath.sh File1.txt 
+1

Was Sie suchen (wie in den Antworten unten beschrieben), wird als "Dateitestoperator" bezeichnet. – user1717259

+0

@ user1717259, Ja! Du hast Recht. – MAK

Antwort

7
#!/bin/sh 

Given_Path=/user/document/workplace/day1 

File_Name=$Given_Path/$1 

#Check if file is present or not. 

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

Einige allgemeine Bedingungen:

-b file = True if the file exists and is block special file. 
-c file = True if the file exists and is character special file. 
-d file = True if the file exists and is a directory. 
-e file = True if the file exists. 
-f file = True if the file exists and is a regular file 
-g file = True if the file exists and the set-group-id bit is set. 
-k file = True if the files' "sticky" bit is set. 
-L file = True if the file exists and is a symbolic link. 
-p file = True if the file exists and is a named pipe. 
-r file = True if the file exists and is readable. 
-s file = True if the file exists and its size is greater than zero. 
-s file = True if the file exists and is a socket. 
-t fd = True if the file descriptor is opened on a terminal. 
-u file = True if the file exists and its set-user-id bit is set. 
-w file = True if the file exists and is writable. 
-x file = True if the file exists and is executable. 
-O file = True if the file exists and is owned by the effective user id. 
-G file = True if the file exists and is owned by the effective group id. 
+2

Sollte das Flag nicht -e sein, da das OP nicht explizit sagt, dass die Eingabe eine reguläre Datei ist? – SilentMonk

+0

wahr, ich habe meine Antwort aktualisiert. –

2
if [ -e "filename" ] 
then 
    echo $1 
else 
    echo "File not found" 
fi 
+2

-e = wenn die Datei existiert. -f = Wenn die Datei existiert und eine Datei ist – elcaos

Verwandte Themen