shell 从用户接收关键字和类别并在该类别的文件中查找关键字的所有示例的脚本

5anewei6  于 2023-08-07  发布在  Shell
关注(0)|答案(1)|浏览(125)

我对剧本不熟悉。在工作环境中,我的任务是创建一个脚本,该脚本接受来自用户的两个值(关键字和类别)。[步骤A]
然后,脚本应该在文件存储库中搜索与类别同名的文件夹。(在此脚本的上下文中,类别将对应于文件夹名称)[步骤B]
找到后,它应该将该文件夹中的文件复制到它创建的新临时文件夹中。[步骤C]**
之后,脚本应该在temp文件夹的文件中搜索关键字的每个示例并返回它们。[步骤D]**
最后一步是删除临时文件夹。[步骤E]**
我知道这些脚本是在Korn Shell中(如果这有任何意义的话)。你们有没有什么提示,让我可以阅读什么文档,以便我可以找到我正在寻找的任何步骤?现在,我有步骤A:

echo "Please enter category"
read category
echo $category

echo "Please enter keyword"
read keyword
echo $keyword

字符串
这是正确的路吗?

wz3gfoph

wz3gfoph1#

我已经提出了一个简单的bash脚本来完成上述任务。您可以根据需要随意更改目录名和文件夹名。你也可以在单独的配置中提及它们,并在主脚本中引用它们。

#!/bin/bash
#Read the input from user 

read -p "Enter a keyWord: " keyword
read -p "Enter a category : " category`

#search for the folder in the mentioned directory. 

folder=$(find /path/to/repo -type d -name "$category")

#create a temp directory 
temp_dir="/path/to/temp_folder" 
mkdir -p "$temp_dir"

#copy the files 
cp -R "$folder"/* "$temp_dir"

#search the instances of the keyword 

grep -r "$keyword" "$temp_dir"

#delete the temp folder 

rm -rf "$temp_dir"

字符串

相关问题