Hello so today i was playing around with my shell script and figured to make it more user friendly i would make it so the file extension of file was automatically added.
for example say the user wants to search a file using grep but first they must type in thhe name of the file in this case lets say file.txt what i want to do is automatically add on the .txt so the user only needs to type in "file"
here is what i have so far but this does not work:
echo "Current .txt files "
ls -R |grep .txt
echo "--------------------------------------------------------------------------------"
echo -n "Please select a file to search in: "
read fileName
file=$fileName.txt
i thought in this case since i am appending an extension on to the end of the variable name but this has not worked.
grep .txtwill likely match more than you wanted; trygrep '\.txt$'. Generally, consider usingfind` for enumerating files in directory subtrees, because it is much more flexible; the equivalent of your command usingfindisfind . -type f -name '*.txt' -exec basename {} +