How would I implement the search function to find a file or directory

女生的网名这么多〃 提交于 2020-01-06 06:07:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!