问题描述
我只是新在Unix中编程,并有一个小问题,我不确定如何解决。这片我的脚本的目的是向用户提供各种选项,以他们想用的扫描类型。这种扫描与检测根据选择的选项中指定的变量重复的文件。
我无法得到它在所有的工作,我不能确定为什么?
也可以请你给我我怎么可能的话可以更好地显示在选择屏幕上的建议。我只贴我的code的一部分,我想弄清楚我的目标,剩下的我自己。
#!/斌/庆典
same_name =1
文件大小=2
的md5sum =3
different_name =4
回声选项列表是,same_name = 1,文件大小= 2的md5sum = 3,不同的名称= 4
回声搜索的文件:
选择读
如果[$选择==$ same_name];然后
查找/主页/用户/ OSN型的F -exec基本名'{}'\\; |排序> Filelist.txt中
查找/主页/用户/ OSN型的F -exec基本名'{}'\\; |排序| uniq的-d> repeatlist.txt
其他
LS -al / home / user中/ OSN> 2filelist.txt
科幻
这将有助于如果您包括您正在接受的错误信息。当我尝试这样做,我得到了一个错误:
./富:9号线:[1:命令未找到
这使得问题相当清楚。在如果
语句 [
经营者,在Unix中的永不使用的东西复杂,当一些简单的黑客将工作的风格,只是一个程序。 (请参阅 LS /斌/ [
的证明!)因此,它需要像命令行选项任何其他程序来对待;你从它与空白选项分开吧。否则,bash中认为[$选择,级联,是要执行的程序的名称,将尝试执行它。因此,该行需要是:
如果[$选择==$ same_name];然后
在我改变了这一切,它的工作。
此外,作为一种风格的建议,我会注意的是,情况
构造是为了写这篇code比使用如果语句,当你有多个测试。而且,正如在其他的答案提到的,你应该把周围的 $选择
,防范案件标记出用户输入为空或包含空格 -
$选择
,不带引号,将扩大到由空格隔开的零个或多个令牌的列表,而$选择
一直扩展到单个令牌。
I am just new to programming in Unix and have a small issue that I am unsure of how to solve. The objective of this piece of my script is to offer the user various options as to the type of scan they would like to use. This scan detects duplicate files with specified variables depending on the option chosen.
I am unable to get it working at all and am unsure why?
Also could you please offer me advice on how I could better display the selection screen if possible. I have only pasted part of my code as I would like to figure out the rest of my objective myself.
#!/bin/bash
same_name="1"
filesize="2"
md5sum="3"
different_name="4"
echo "The list of choices are, same_name=1, filesize=2, md5sum=3 and different name=4"
echo "Search for files with the:"
read choice
if [$choice == "$same_name" ];then
find /home/user/OSN -type f -exec basename '{}' \; | sort > filelist.txt
find /home/user/OSN -type f -exec basename '{}' \; | sort | uniq -d > repeatlist.txt
else
ls -al /home/user/OSN >2filelist.txt
fi
It would help if you included the error messages you were receiving. When I tried this, I got an error of:
./foo: line 9: [1: command not found
This makes the problem fairly clear. The [
operator in the if
statement is, in Unix's "never use something complicated when some simple hack will work" style, just another program. (See ls /bin/[
for proof!) As such, it needs to be treated like any other program with command-line options; you separate it from its options with whitespace. Otherwise, bash will think that "[$choice", concatenated, is the name of a program to execute and will try to execute it. Thus, that line needs to be:
if [ $choice == "$same_name" ];then
After I changed that, it worked.
Also, as a style suggestion, I'd note that the case
construct is a much easier way to write this code than using if
statements, when you've got more than one test. And, as noted in other answers, you should put "
marks around $choice
, to guard against the case where the user input is empty or contains spaces -- $choice
, unquoted, will expand to a list of zero or more tokens separated by whitespace, whereas "$choice"
always expands to a single token.
这篇关于如果从错误和壳else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!