在文本编辑器中创建 shell 脚本

sha*_*ard 1 bash shell-script

我想知道如何在文本编辑器中创建 shell 脚本。

所以这就是我在文本编辑器中的内容。

#!/bin/bash
mkdir -p temp
cd temp

if [ $1 > $2 ] ;
then
    echo $1
else
    echo $2
fi

./max.sh 4 6
./max.sh -2 -5
./max.sh 7 -3
Run Code Online (Sandbox Code Playgroud)

所以基本上在文本编辑器中,我想创建一个名为 max.sh 的 shell 脚本,以便在它下面我可以通过它但在同一个文本编辑器中传递参数。

为了更清楚地说明:

我希望 if 语句位于名为 max.sh 的脚本中,因此在它下面我可以使用参数调用 max.sh 并且它会起作用。

Wil*_*ard 10

你想要的是一个函数

#!/bin/bash

max() {
  if [ "$1" -gt "$2" ] ;
  then
    printf %s\\n "$1"
  else
    printf %s\\n "$2"
  fi
}

max 4 6
max -2 -5
max 7 -3
Run Code Online (Sandbox Code Playgroud)

进一步阅读: