![](/img/trans.png)
[英]How to create a shell script which can create a .java file on executing?
[英]How to create a shell script that can scan a file for a specific word?
我为我的计算机科学GCSE要做的一个问题是:
编写一个shell脚本,该脚本接受用户输入的字符串,询问文件名并报告该字符串是否存在于文件中。
无论如何尝试执行此操作,都无法创建Shell脚本。 我不需要您告诉我全部数字,但是,我不知道从哪里开始。 我输入了变量和文件名,但是,我不知道如何在所选文件中搜索所选单词。 有任何想法吗?
例如,使用grep可以使它正常工作
viewEntry()
{
echo "Entering view entry"
echo -n "Enter Name: "
read input
if grep -q "$input" datafile
then
echo ""
echo -n "Information -> "
grep -w "$input" datafile
echo ""
else
echo "/!\Name Not Found/!\\"
fi
echo "Exiting view entry"
echo ""
}
dataFile是您将要读取的文件。 然后,使用grep的-q和-w参数,您应该能够浏览所选的文件。
这个站点在解释grep和您的确切问题方面做得很好: http : //www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
下面的shell脚本是一种非常快速的方法来完成您建议的操作:
#!/bin/sh # Tell your shell with what program this script should be exectued
echo "Please enter the filename: "
read filename # read user input into variable filename
count=`grep -c $1 $filename` # store result of grep into variable count
if [ $count -gt 0 ] # check if count is greater than 0
then
echo "String is present:" $1
else
echo "String not found:" $1
fi
您应该看一些教程以获取shell脚本的基础知识。 您的任务不是很复杂,因此阅读一遍之后,您应该能够了解脚本的作用并根据需要进行修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.