Script that takes in a keyword and a category from user and finds all instances of keyword in files of that category

huangapple go评论44阅读模式
英文:

Script that takes in a keyword and a category from user and finds all instances of keyword in files of that category

问题

以下是已经翻译好的部分:

我对脚本不太了解。在工作背景下,我被要求创建一个脚本,该脚本从用户那里获取两个值(关键字和类别)[步骤A]

然后,脚本应该在文件库中搜索具有与类别相同名称的文件夹。(在这个脚本的上下文中,类别将对应于文件夹名称)[步骤B]

一旦找到,它应该将该文件夹中的文件复制到一个新的临时文件夹,然后创建它。[步骤C]

之后,脚本应该在临时文件夹的文件中搜索关键字的每一个实例并将它们返回。[步骤D]

最后一步是删除临时文件夹。[步骤E]

我知道脚本是使用Korn Shell编写的(如果这有任何意义的话)。您们是否有关于我可以阅读哪些文档以找到我需要的任何步骤的建议?目前,我对步骤A有以下内容:

echo "请输入类别"
read category
echo $category

echo "请输入关键字"
read keyword
echo $keyword

这是正确的方法吗?

英文:

I'm new to scripts. In the context of work, I was tasked with creating a script that takes in two values from the user (keyword and category).[Step A]

Then, the script should search a repo of files for the folder that has the same name as the category. (In the context of this script, a category will correspond to a folder name) [Step B]

Once found, it should copy the files in that folder to a new temporary folder that it creates. [Step C]

After that, the script should search for every instance of the keyword in the files of the temp folder and return them. [Step D]

The last step is to delete the temporary folder. [Step E]

I know the scripts are in Korn Shell(if that makes any sense). Do you guys have any tips on what documentation to read so that I can find what I'm looking for for any of the steps? At the moment I have this for Step A:

echo "Please enter category"
read category
echo $category

echo "Please enter keyword"
read keyword
echo $keyword

Is this the correct way to go?

答案1

得分: 0

我制作了一个简单的bash脚本来执行上述任务。
请随意更改目录名称和文件夹名称,如有需要,也可以在单独的配置中提及并在主脚本中引用。

#!/bin/bash
#从用户处读取输入

read -p "输入关键词:" keyword
read -p "输入类别:" category

#在指定目录中搜索文件夹

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

#创建临时目录
temp_dir="/path/to/temp_folder"
mkdir -p "$temp_dir"

#复制文件
cp -R "$folder"/* "$temp_dir"

#搜索关键词的实例
grep -r "$keyword" "$temp_dir"

#删除临时文件夹
rm -rf "$temp_dir"
英文:

I have come up with simple bash script that does the above tasks.
Feel free to change the directory names and folder names as required. you can also mention them in separate configs and refer them in the main script.

#!/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"

huangapple
  • 本文由 发表于 2023年7月3日 21:19:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定