Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

Hopefully this makes sense, sorry if it doesn't

Any help is appreciated.

share|improve this question
up vote 10 down vote accepted

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:

share|improve this answer
    
thanks man, really appericate it, it worked – shawn edward Mar 3 at 23:21

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.

share|improve this answer
    
this works as well, thanks for the suggestion, appreciate it – shawn edward Mar 3 at 23:42
1  
@shawnedward, do be careful with this if you use it, though—a fixed name temp file is a security hole. – Wildcard Mar 3 at 23:52

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.