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
  • 55,929
  • 26
  • 146
  • 227
shawn edward
  • 473
  • 3
  • 13
  • 17

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
  • 35,316
  • 26
  • 130
  • 258
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.

  • this works as well, thanks for the suggestion, appreciate it – shawn edward Mar 03 '16 at 23:42
  • 1
    @shawnedward, do be careful with this if you use it, though—a [fixed name temp file is a security hole](http://unix.stackexchange.com/q/235985/135943). – Wildcard Mar 03 '16 at 23:52
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.

Arif Burhan
  • 101
  • 1