2017-08-17 47 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绝不一样?)