English 中文(简体)
错误,如果是的话,否则的话
原标题:Errors from if and else statements in shell
  • 时间:2010-03-09 16:54:26
  •  标签:
  • bash
  • shell

我刚刚在“九州”进行方案规划,并有一个我无法解决的小问题。 我的这篇文字的目的是向用户提供他们希望使用的扫描类型的各种选择。 该扫描仪根据所选择的选择,发现有特定变量的重复档案。

我根本不能让它工作,不知为什么?

另外,请让我就如何更好地展示甄选屏幕提出建议。 我只重复了我守则的部分内容,因为我要说我的目标其余部分。

#!/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
最佳回答

如果列入你收到的错误信息,将会有所帮助。 在我尝试时,我犯了一个错误:

./foo: line 9: [1: command not found

这使问题变得相当清楚。 The [mente> statement is, in Unix s “never use some brief hack will work” Format, only another program. (见ls /bin/[ for evidence!”) 因此,它需要像任何其他具有指挥权选择的方案一样对待;你将其与选择白天分开。 否则,bash将认为,“[钱财”是执行方案的名称,并将试图执行该方案。 因此,这一方针需要:

if [ $choice == "$same_name" ];then

在我改变后,它发挥了作用。

此外,作为风格建议,我还注意到,“<条码>>>><>条码/代码>的构造比“<条码/代码>在你进行不止一次测试时使用<条码>说明更为容易地书写这一代码。 如其他答复所指出,你应当将<代码><<>>>>>>>>> 贴在$choice>/code>上,以防用户投入空或包含空间的情形——$choice,不予引用,将扩大到由白色空间分离的零或更多象征性物清单,而$choice”则总是扩大到单一象征性。

问题回答

单壳指令[,又称test ,在后需要一个空间,才能正确穿透。 例如:

if [ "x$choice" == x"$same_name" ] ; then

等于

if test "x$choice" == "x$same_name" ; then

将“x”预先分配给变量是防止<代码>测试<<>>>>/代码>的异构体。 如被称作<条码>测试5=,则如果<条码>> $/条码>和<条码>same_ 姓名是空话,则试验会抱怨。

也可使用<条形码>月值:-default}或$-{choice:=default},以防止无固定变量或无壳变量。

如果您使用[(或test>),则显示平等的经营者为=而不是=

你可以这样做。

while true
do
cat <<EOF
The list of choices are:
  1) same name
  2) filesize
  3) md5sum
  4) different name
  5) exit
EOF
read -r -p "Enter your choice: " choice
case "$choice" in
 1)
    find /home/user/OSN -type f -exec basename  {}  ; | sort > filelist.txt
    find /home/user/OSN -type f -exec basename  {}  ; | sort | uniq -d > repeatlist.txt
 5) exit;
 *) ls -al /home/user/OSN  >2filelist.txt
esac
done

Bash的双重方括号更有利于引用和取消或确定变数。

if [[ $choice == "$same_name" ]]; then

请参看Bash sselect>::

choices="same_name filesize md5sum different_name exit"

PS3="Make a selection: "    # this is the prompt that the select statement will display
select choice in $choices
do
    case $choice in
        same_name)
            find ...
            ;;
        filesize)
            do_something
            ;;
        .
        .
        .
        exit)
             break
             ;;
    esac
done




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签