问题
I am currently working on a script for bash that will ask the user for a starting location and a file name then search for the file. If possible I would also like to cat the file once found. Here is the script I have so far if someone would point out how I would include the function I would be very thankful. Also still a Unix beginner so please bear with me :)
#!/bin/bash
function press_enter
{
echo""
echo -n "Press Enter to continue"
read
clear
}
selction=
until [ "$selection" = "3" ]; do
echo -e "Where would you like to search
1- Root
2- Home
3- Exit
Enter your choice ---> \c"
read selection
case $selection in
1) cd / ; press_enter ;;
2) cd /home ; press_enter ;;
3) echo "Have a nice day!" ; exit ;;
esac
done
回答1:
You can use find as one command, or use your selection inputs to drive find
in your script.
find $where_selection -name $what_selection
# with cat:
find $where_selection -name $what_selection -exec cat {} \;
-exec cat {}
replaces the {}
with the current found file path from find
.
来源:https://stackoverflow.com/questions/47332967/how-would-i-implement-the-search-function-to-find-a-file-or-directory