Команды SFK


    1        2        3        4        5        6        7        8        9        10    

Раздел 10. Help - Помощь
help chain | help chars | help colors | help compile | help nocase | help office | help options | help patterns | help select | help shell | help unicode | help var | samp |


Help:   Рус   |   Eng        Refer:   Рус   |   Eng  

Команда: help var

How to set and read variables in SFK scripts

to extract data in one command and reuse it in following commands.
sfk parameters and variables support

sfk script parameters

   -  look like %1 %2 %3 to %9,
      or with sfk for windows also like $1 $2 $3.

   -  are used with sfk script and call / label.

   -  are passed into the script or label command chain
      wherein they are never changed.

web reference
   http://stahlworks.com/sfk-helpvar

   example:
      --- file filt.bat begin ---
      sfk script "%~f0" -from begin %*
      GOTO end
      sfk label begin
         +filter %1 %2
         +end
      :end
      --- file filt.bat end ---
      typing "filt.bat in.txt -+foo" will run sfk filter
      using the parameters "in.txt" and "-+foo".
      under windows %~f0 is the absolute batch filename
      itself including extension .bat or .cmd.

sfk global variables

   -  are set like:
      sfk setvar myvar="the test text" ...
      sfk echo foo +setvar myvar ...
      sfk xed in.txt "/foo*bar/[setvar myvar][part2][endvar]/" ...

   -  are used by further commands in the chain like:
      sfk ... +getvar myvar
      sfk ... +echo -var "using #(myvar)"
      sfk ... +xed "_<foo>*</foo>_[part1][getvar myvar][part3]_"
      note:
        to read or use variable contents by a pattern #(name)
        option -var must be given. this is to avoid unwanted
        side effects with commands that get "#(" in their
        input files or input text streams.

   -  allowed variable names:
      must start with a-z, then a-z0-9_

   examples:
      sfk setvar file=in.txt +filter -var "#(file)" -+foo
         runs sfk filter, giving the input filename by variable.
      sfk xex in.txt "/foo=*/[setvar fooval][part2][endvar]/"
       +echo -var "foo is: #(fooval)"
         extract foo=(any text) from in.txt, place the found
         text into variable fooval, then print it. [19]

sfk predefined variables

   #(sys.slash)    produces \ under windows, / under linux.
   #(sys.sfkver)   current sfk version.
   #(sys.numcols)  number of console columns.
   #(sys.ownscript.name)  filename of current script.

   to get the text of the current script, use for example:
   sfk ... +getvar sys.ownscript.text +filter ...

   environment variable access

   can be done like #(env.varname). varname is case
   insensitive under windows and uses case on linux.

   example:
      sfk -var echo "tmp contains: #(env.TMP)"

sfk local command variables

   -  are created directly from input text
      produced by a previous command in the command chain

   -  are suppported only within some commands like
      sfk run "... $text ...."
         runs an external program once for every input line.
      sfk perline "... $text ..."
         runs sfk internal commands once for every input line.
      sfk filter -tabform "... $col1 ... $col2 ..."
         splits text lines by TAB char, allowing reformatting.
      type sfk run, sfk perline etc. for further infos.

see also
   sfk setvar    set an SFK variable
   sfk addtovar  append text lines to an SFK variable
   sfk incvar    increment a numeric variable
   sfk decvar    decrement a numeric variable
   sfk getvar    get SFK or environment variable

sfk variable output formatting

   formal syntax:

      #(-03.4varname)

   with possible control characters:
      -   format left justified, else right
      0   fill with zeros, else with blanks
      .4  take up to 4 chars from variable

   example: if variable i contains "1" then

      command                 output
      +echo -var ">#(i)<"     >1<
      +echo -var ">#(3i)<"    >  1<
      +echo -var ">#(-3i)<"   >1  <
      +echo -var ">#(03i)<"   >001<

   example: if variable s contains "abcde" then

      command                 output
      +echo -var ">#(.3s)<"   >abc<
      +echo -var ">#(5.3s)<"  >  abc<

sfk variable functions

   when reading variable text like #(varname) some extra
   functions can be applied using #(func(varname,...)).
   available functions are:

   strpos(v,'text')        get index of text within v.
                           0=first char, -1=not found
   strpos(v,-case 'text')  same, case sensitive (fast)
   strpos(v,myvar)         get index of text from myvar
                           within text of variable v.
   strpos(v,-spat '\x20')  search using slash patterns
   strrpos(v,'text')       search from right side
   contains(v,'text')      tells 1 if text is found in v,
                           else 0. accepts -case and -spat
   contains(v,-case a)     tells if text from variable a
                           is contained within v using
                           fast case sensitive search
   begins(v,'word')        check if string starts with word.
                           returns 1 (yes) or 0 (no).
   ends(v,'word')          check if string ends with word.
                           returns 1 (yes) or 0 (no).
   substr(v,o[,l])         substring from offset o length l
                           which can be variables themselves.
                           offset 0 is first char. negative o
                           starts from right side minus o.
   rsubstr(v,o[,l])        substring from right side taking
                           up to l chars in left direction.
   [l/r]trim(v)            strip whitespace at sides
   isset(v)                tells 1 if v is set, else 0
   size(v)                 number of bytes in v
   strlen(v)               number of characters in v,
                           if it contains just plain text
   numlines(v)             number of lines in v

   example: if variable s contains "foo bar" then

   command                           output
   +echo -var "#(substr(a,4,3))"     bar
   +echo -var "#(strpos(a,'bar'))"   4