1

I want to know how to create a shell script inside a text editor.

So this is what I have inside the text editor.

#!/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

So basically inside the text editor I want to create a shell script called max.sh so that below it I can pass arguments through it but in the same text editor.

To make it more clear:

I want the if-statement to be inside a script called max.sh, so below it I can call the max.sh with arguments and it will work.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

3 Answers3

10

What you want is called a function:

#!/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

Further reading:

Wildcard
  • 36,499
3

You can do what you ask like this:

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

cat <<\_script_lines_ > max.sh
#!/bin/bash
if [ "$1" -gt "$2" ] ;
then
    printf '%s\n' "$1"
else
    printf '%s\n' "$2"
fi
_script_lines_

chmod u+x max.sh             ### make the script excutable.

# Use the script:
./max.sh 4 6
./max.sh -2 -5
./max.sh 7 -3

But the function already recommended by Wildcard seems more reasonable to use.

0

If you feel up to programming it you can get a script to open a second or even third Console Window and use these for Input/Output, like reading,writing from other files but using Consoles instead.

I don't know what the syntax on bash is, you would need to google it or ask again on stackoverflow.