2017-08-17 175 views
0

這是我當前的代碼部分執行上./不SH

#! /bin/bash 

#Take no arguments in 
#checks to see if home/deleted is in existence 
#creates the directory or file if it is missing 
**(line 15)** function checkbin(){ 

    if [ ! -c "~/deleted" ]; then 
      mkdir -p ~/deleted 
    fi 
    if [ ! -f "~/.restore.info" ]; then 
      touch ~/deleted/.restore.info 
    fi 

} 

我可以把這個代碼可以正確使用./remove [ARGS]
但是當我打電話使用sh remove [ARGS]
我收到以下錯誤remove: 15: remove: Syntax error: "(" unexpected

ls -l對文件-rwxr-x--x

unix是否支持sh和./的執行?

./ /斌/慶典用於執行時(如在定義認領),而 sh可以是另一種解釋器或打壞,其可以根據它是如何被調用 sh具有不同的特性的鏈路
+1

SH將在Bourne shell中運行,而你想在bash執行。 –

+0

調用'bash remove [ARGS]'或重寫腳本sh。 – beliy

回答

1

bash的是從SH衍生,但有一些特定的語法:

例如在SH Function Definition Command

fname() compound-command[io-redirect ...] 

而不function關鍵字。

詳情

Bash

Posix shell

1

如果你希望你的腳本在sh以及在bash運行,你需要寫POSIX標準的外殼。

在這種情況下,這意味着不使用猛砸function關鍵字:

checkbin(){ 
    test -c "~/deleted" || mkdir -p ~/deleted 
    test -f "~/.restore.info" || touch ~/deleted/.restore.info 
} 

如果你正在編寫可移植的shell,那麼它的使用#!/bin/sh爲您的家當是一個好主意。

(順便說一句,我假設你知道"~/deleted"~/deleted絕不一樣?)