2013-03-05 115 views
3

當我嘗試運行該腳本時,出現「第9行:[2:command not found」錯誤。這是我的第一個bash腳本,所以我是一個完整的初學者。有人能告訴我爲什麼這個bash腳本不會運行嗎?

#!/bin/bash 

num1=0 
num2=1 

count=2 


while [$count -le $1] 
do 
num3='expr $num1+$num2' 
num1=$num2 
num2=$num3 
count='expr $count+1' 

done 

echo "Fib Num: $num3" 

回答

3

試試這個:

$ cat t.sh 

表演:

#!/bin/bash 

num1=0 
num2=1 

count=2 

while [ $count -le $1 ]; do 
    num3=$(expr $num1 + $num2) 
    num1=$num2 
    num2=$num3 
    let count++; 
done 

echo "Fib Num: $num3" 

結果:

$ bash t.sh 9 
Fib Num: 34 

[]需要空間, expr命令應該被打包在$()之內,而不是單引號。你可能打算使用反引號(`),但不要再使用反引號。

由於您使用的是bash,因此您可以簡化增加計數,如圖所示。

+0

這是令人沮喪的..即時通訊「行10:[:2:一元操作符預計」現在 – user2012751 2013-03-05 22:51:51

+0

你如何運行腳本? – 2013-03-05 22:53:24

+0

@ user2012751:嘗試在'$ count'和'-le'和'$ 1'之間移除while條件中的空格:'while [$ count-le $ 1]'。它現在工作嗎? – 2013-03-05 22:54:49

5

[]附近增加空格。 [是一個命令,所以它必須是一個單獨的詞。從man bash

test expr 
[ expr ] 
     Return a status of 0 or 1 depending on the evaluation of the 
     conditional expression expr. Each operator and operand must be 
     a separate argument. Expressions are composed of the primaries 
     described above under CONDITIONAL EXPRESSIONS. 
+0

現在,我收到了「第10行:[:2:一元運算符預期」 – user2012751 2013-03-05 22:37:35

+0

這也是很好的補充'設置-e'中止對錯誤的腳本執行。並設置-x來了解正在發生的事情。 – AxelOmega 2013-03-05 22:42:13

+1

「一元運算符預期」可能是由於$ 1爲空。在這之前你需要檢查它不是空的。 – Idelic 2013-03-05 22:43:30

1

作爲參考,這裏有一些類似於「常見」的理想答案。

#!/usr/bin/env bash 

# Bash/ksh93 
# Theoretically also zsh/mksh, but both have bugs that break it. 
function fib1 { 
    # It is critical to validate user input if it will be used in an arithmetic 
    # expression. 
    [[ $1 == +([[:digit:]]) ]] || return 1 

    # Demonstrates an accumulating version to print the intermediary results. 
    typeset i=1 f=(0 1) 
    while ((i < $1)); do 
     # Don't use expr(1) unless you're coding for Bourne. 
     ((f[i] = f[i-1] + f[i++])) 
    done 
    echo "${f[@]}" 
} 

fib1 "$1" 

這是令人困惑的是,除了破碎的語法,你用奇怪expr即使有/bin/bash家當。你應該閱讀bash手冊,而不是閱讀給你的任何建議。

#!/bin/sh 

# POSIX (posh/bash/zsh/all kshes) 
# It's easier to write a let wrapper than re-type test expressions repeatedly. 
# This emulates let precisely, but it's not important to understand everything 
# going on here. Breaks in dash because of its lack of support for the 
# otherwise ubiquitously supported comma operator, and busybox because of a bug in 
# its environment assignments. 
let() { 
    IFS=, command eval test '$(($*))' -ne 0 
} 

fib2() { 
    # Uglier validation for POSIX 
    case $1 in 
     *[^[:digit:]]*|'') 
      return 1 
    esac 

    # Using globals for simplicity 
    count=$1 n1=0 n2=1 
    while let count-=1; do 
     let n1=n2 n2=${n1}+n2 
    done 

    printf 'Fib Num: %d\n' "$n2" 
} 

fib2 "$1" 

我的算術斐波那契有點好奇。

# Bash/ksh93/zsh 
# Could be more elegant but includes bug workarounds for ksh/zsh 
$ a=a[++n%20]=a[n]+a[n-1],a[0] a[n=2]=1; let a a=0; echo "${a[@]}" 
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 

# Less portable because of bugs. Works in Bash. 
$ a=('a[a[n]=a[n-1]+a[n-2],n++/20]' 2 0 1); echo "${a[@]:n=4,a}" 
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 
+0

感謝您的支持。對於「完全初學者」來說,這可能有點多,但這是一項非常有價值的研究。很好的使用'(('和'))';驗證是一個好主意。希望我可以多投這個票。 – 2013-03-06 17:41:39

相關問題