| 1 | #!/usr/local/bin/ysh
|
| 2 | #
|
| 3 | # "Learn YSH in Y Minutes"
|
| 4 | #
|
| 5 | # Based on "Learn Bash in Y Minutes"
|
| 6 | #
|
| 7 | # The first line, starting with #! is the shebang, which tells the system how
|
| 8 | # to execute the script: https://en.wikipedia.org/wiki/Shebang_(Unix)
|
| 9 | #
|
| 10 | # Comments start with #
|
| 11 |
|
| 12 | # Hello world:
|
| 13 | echo "Hello world!" # => Hello world!
|
| 14 |
|
| 15 | # Each command starts on a new line, or after a semicolon:
|
| 16 | echo 'first line'; echo 'second line'
|
| 17 | # => first line
|
| 18 | # => second line
|
| 19 |
|
| 20 | # Declaring a variable looks like this:
|
| 21 | var myvar = 'Some string'
|
| 22 |
|
| 23 | # Using the variable:
|
| 24 | echo $myvar # => Some string
|
| 25 | echo "$myvar" # => Some string
|
| 26 | echo '$myvar' # => $myvar
|
| 27 |
|
| 28 | # When you use the variable itself — assign it, export it, or else — you write
|
| 29 | # its name without $. If you want to use the variable's value, you should use $.
|
| 30 | # Note that ' (single quote) won't expand the variables!
|
| 31 |
|
| 32 | # Parameter expansion ${ }:
|
| 33 | echo ${myvar} # => Some string
|
| 34 |
|
| 35 | # Substring from a variable using Python-like slices.
|
| 36 | var n = 7
|
| 37 | echo $[myvar[0:n]] # => Some st
|
| 38 | # This will return only the first 7 characters of the value
|
| 39 | echo $[myvar[:-5]] # => tring
|
| 40 | # This will return the last 5 characters (note the space before -5)
|
| 41 |
|
| 42 | # String length
|
| 43 | echo ${#myvar} # => 11
|
| 44 |
|
| 45 | # Default value for variable
|
| 46 | echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
|
| 47 | # => DefaultValueIfFooIsMissingOrEmpty
|
| 48 | # This works for null (Foo=) and empty string (Foo=""); zero (Foo=0) returns 0.
|
| 49 | # Note that it only returns default value and doesn't change variable value.
|
| 50 |
|
| 51 | # Declare an array with 6 elements
|
| 52 | var array0 = :| one two three four five six |
|
| 53 |
|
| 54 |
|
| 55 | #
|
| 56 | # Stopped translating here, not sure I like this format.
|
| 57 | # It's good for some languages, but too long for bash.
|
| 58 | #
|
| 59 | return
|