Сообщения

Сообщения за январь, 2018

Bash Shell: Check File Exists or Not

H ow do I test existence of a text file in bash running under Unix like operating systems? You need to use the test command to check file types and compare values. The same command can be used to see if a file exist of not. The syntax is as follows: test -e filename [ -e filename ]   test -f filename [ -f filename ] The following command will tell if a text file called /etc/hosts exists or not using  bash conditional execution  : [ -f / etc / hosts ] && echo "Found" || echo "Not found" Sample outputs: Found The same code can be converted to use with  if..else..fi  which allows to make choice based on the success or failure of a test command: #!/bin/bash file = "/etc/hosts" if [ -f " $file " ] then echo " $file found." else echo " $file not found." fi File test operators The following operators returns true if file exists: -b FILE ...