2016-05-31 1 views
1

Unix 쉘 스크립팅을 처음 사용합니다.주어진 경로에 파일이있는 경우 파일 표시

하나의 매개 변수를 파일의 주어진 경로에서 찾기 위해 파일 이름으로 사용하는 파일을 만들었습니다. 파일이 발견되면 파일을 표시하거나 적절한 메시지를 표시해야합니다.

파일 : 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 

실행 : 실행 파일

$ bash FilePath.sh File1.txt 
+1

(아래 답변에 설명 된대로) '파일 테스트 연산자'라고합니다. – user1717259

+0

@ user1717259, 예! 너 맞아. – MAK

답변

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 

일부 일반 조건 :

-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

OP가 입력이 일반 파일이라고 명시 적으로 말하지 않으므로 플래그가 -e가 아니어야합니까? – SilentMonk

+0

사실, 대답을 업데이트했습니다. –

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

-e = 파일이있는 경우. -f = 파일이 존재하고 파일 인 경우 – elcaos

관련 문제