Views:
5,176β
Votes: 2β
Tags:
command-line
bash
scripts
xubuntu
sh
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1142370
Title:
Shell script error to check if file exists
ID:
/2019/05/11/Shell-script-error-to-check-if-file-exists
Created:
May 11, 2019
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
I made a few changes to your script:
# !/bin/bash
although it works is unconventional. Use#!/bin/bash
instead.- Filename is one word but often we think it is two. Consequently it was spelled differently as
file-name
andfile_name
- Cosmetically lines between
if
->else
->fi
should be indented for greater readability. It still works vertically aligned itβs just easier to read when indented. - Extra words in
Enter the name of the file
can be shortened toEnter filename
to make programs shorter and faster. This also makes it quicker for people to read instructions.
#!/bin/bash
echo -e "Enter filename: \c"
read filename
if [ -e "$filename" ]
then
echo "$filename found"
else
echo "$filename not found"
fi