save partly file content to env var - how?
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    connor
    Posts: 570 from 2007/7/29
    Sometimes I want to grab a text from a file and save it in an env var but I do not know how to do it. I can save it to any file with ">" operator to RAM: or other places but that's not what I want. I tried to fiddle with "$myname" which is possible to set via the SETENV command, but how can I do something like:
    search filename string >$myvar

    correctly? I tried to concatenate "search filename string" with "|" operator to then set the env var but that also did not work. Can someone give me a hint?

    Or is there something like local variables inside the Shell like in MS-DOS that can be reused instantly?
  • »15.03.13 - 17:26
    Profile
  • jPV
  • Yokemate of Keyboards
    Yokemate of Keyboards
    jPV
    Posts: 2074 from 2003/2/24
    From: po-RNO
    env variables are stored as normal files, so you can redirect output into them too.
    search filename string > ENV:myvar
    search filename string > ENVARC:myvar
    ENV: is what's in use in current session and ENVARC: is stored for permanent use after reboot...

    So.. do you really need some other option or could that just do? :)
  • »15.03.13 - 18:26
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    boot_wb
    Posts: 874 from 2007/4/9
    From: Kingston upon ...
    Hi Connor,

    Within scripts you can use Set (rather than setenv) for local variables.

    To store search results within an env-var you could use:
    search >env:myvar filename string
    or
    search >env:myvar filename PATTERN string
    (to use pattern matching functions)

    Search will return (or store into myvar in the example above) the full line of text on which the string is found, rather than just the string searched for.

    Subsequently, you use "env:myvar" to reference the file itself, and "$myvar" to reference the contents (ie the stored string).
    eg:
    Quote:

    if exists env:myvar
    dofunkystuff
    endif

    (Does stuff if myvar exists
    or
    Quote:

    if EQ $myvar 1
    dofunkystuff
    endif

    (Does stuff if the contents of myvar equal "1"

    NB - Unlike setenv (which will always create/write-to the file in env:), redirecting output using the ">" operator can go to any file, (defaulting to the current directory from which the script is run), so you may want to redirect to an explicit path (eg ">env:myvar" rather than just ">myfile".

    The Amigados manual may be useful.
    There's also a document on back2roots.org summarising pattern matching quite well, but it's in doc format. Just serach "Amigados pattern matching" in google to find it.

    Best regards


    Rich
    www.hullchimneyservices.co.uk

    UI: Powerbook 5,6 (1.67GHz, 128MB VRam): OS3.1, OSX 10.5.8
    HTPC: Mac Mini G4 (1,5GHz, 64MB VRam): OS3.1 (ZVNC)
    Audiophile: Efika 5200b (SB Audigy): OS3.1 (VNC + Virtual Monitor)

    Windows free since 2011!
  • »16.03.13 - 16:23
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    connor
    Posts: 570 from 2007/7/29
    @ jpv

    Best would be to redirect the output immediately or reuse it as a Shell parameter. But I do not know how to do it with AmigaDOS.

    @ boot_wb

    I usse this book from time to time but I could not succeed yet in what I was trying.


    I did this:
    Code:
    Ram Disk:> search MOSSYS:textfile app
    67> application45
    Ram Disk:> search MOSSYS:textfile app >env:blubber
    Ram Disk:> type $blubber
    TYPE: can't open 67>
    Objekt nicht gefunden
    Ram Disk:> type env:blubber
    67> application45
    Ram Disk:> search ?
    FROM/M,SEARCH/A,ALL/S,NONUM/S,QUIET/S,QUICK/S,FILE/S,PATTERN/S,CASE/S,LINES/N:
    Gefordertes Argument fehlt
    Ram Disk:> search MOSSYS:textfile app >env:blubber nonum
    Ram Disk:> type env:blubber
    application45
    Ram Disk:> execute env:blubber
    Ram Disk:> search MOSSYS:textfile app >env:blase nonum; execute env:blase
    Ram Disk:> search MOSSYS:textfile app >env:blase nonum | execute env:blase
    Ram Disk:> search MOSSYS:textfile app >env:blase nonum > "execute env:blase"

    The 4th last line allows me to start the program that was found by SEARCH command. But this way I have to use two commands. So I combined them on 3rd last line to one line using ";". From what I know this is to apply several commands one after another. But it does not work and I get no output and the command is not started. Wrong usage? Also the last two lines did not do what I want. But I did not expect them to do what I want to achieve. I tried them to find out what the mistake may be. So how can I redirect the out put of the command directly to a command that is executed?

    And can someone please erase this spam idiot and his comments?
  • »19.03.13 - 19:07
    Profile
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    boot_wb
    Posts: 874 from 2007/4/9
    From: Kingston upon ...
    Type is almost certainly not what you want, since this just outputs text similar to echo in this context.
    I'd forgotten about the nonum switch of search, useful for your needs.

    ; seperates commands form comments, so everythihg after this is ignored.
    I'm not sure how (or if its possible) to concatenate command lines in Amigados, scsripting would be my first approach. Since only the filename searched for will change, you could save the following as ram:foosearch :
    Quote:


    .key filename
    .bra {
    .ket }

    search MOSSYS:textfile "{filename}" >env:blubber nonum
    execute env:blubber



    The call it from the cli with:
    execute foosearch [filename]
    (replacing [filename] with your search string.)

    Regards


    Rich

    [ Edited by boot_wb 19.03.2013 - 19:58 ]
    www.hullchimneyservices.co.uk

    UI: Powerbook 5,6 (1.67GHz, 128MB VRam): OS3.1, OSX 10.5.8
    HTPC: Mac Mini G4 (1,5GHz, 64MB VRam): OS3.1 (ZVNC)
    Audiophile: Efika 5200b (SB Audigy): OS3.1 (VNC + Virtual Monitor)

    Windows free since 2011!
  • »19.03.13 - 19:27
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    igracki
    Posts: 411 from 2003/2/25
    From: Berlin
    Hi,

    if you just want to run the search result, you can do this:
    Run `search MOSSYS:textfile app NONUM`

    You have to put the search command in backticks, so the command gets replaced with the result.
  • »19.03.13 - 19:41
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    connor
    Posts: 570 from 2007/7/29
    Thanks for the answers.

    @ boot_wb

    This is not what I want but it may help me anytime later.

    @ igracki

    Yes, this does what I want.
  • »21.03.13 - 19:08
    Profile
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    connor
    Posts: 570 from 2007/7/29
    Okay, running these commands works. but what if I want to use them as arguments for other commands? Say I want to give the search term to ED, MULTIVIEW, FILENOTE, MPLAYER or whatever. Then the backtick quoting does not work. Does it only work for the RUN command? What do I have use for other commands? Tried again single and double quote (normal) but did not succeed yet.
  • »22.03.13 - 17:33
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    igracki
    Posts: 411 from 2003/2/25
    From: Berlin
    Here it works with Ed, MultiView, Filenote, MPlayer, etc.
    I just tried it!

    Are you sure you did it right?

    Here is what I tried:

    Ed `search MOSSYS:textfile app NONUM`
    MultiView `search MOSSYS:textfile app NONUM`
    Filenote `search MOSSYS:textfile app NONUM` TestComment
    MPlayer `search MOSSYS:textfile app NONUM`
  • »25.03.13 - 16:56
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    connor
    Posts: 570 from 2007/7/29
    I just replaced the command and not the arguments, same as you did, so I
    guess I am right.

    I did a textfile SYS:commands.txt with some simple DOS commands:
    Code:

    dir
    list
    newcli
    date
    ed
    ifconfig
    join
    lha
    openurl
    uptime
    version


    I want to give the search term to a command and this does not work. For
    ed `search sys:commands.txt nonum lha`
    it opens an Ed window with the given name to be save. But what I want is to
    give the search string to the content of the file, not to the name. So I
    want the Ed window to open with the search term as content of it, not with
    an empty window. Maybe it was not clearly said.
    Same for Multiview but I guess this does not work because Multiview can only
    handle files. It also says "Could not load files"
    Giving a text string to MPlayer does not make sense, that's clear. I just
    named it as an example for maybe another exercise.
    RUN works but the others not.
    Code:

    Ram Disk:> ed `search sys:commands.txt nonum lha`
    Ram Disk:>
    Ram Disk:> multiview `search sys:commands.txt nonum lha`
    Could not load files.


    Say I want to touch a new file with the command name and set this name also
    as filenote (just for an exercise). then I need to do it like this:
    Ram Disk:> touch lha | filenote `search sys:commands.txt nonum lha`
    laberhannes
    lha..done

    That way I have two times the command "lha". Can I make a call to use only
    one time this parameter?

    Ah, and is it also possible to pick up older commands in the Shell history
    by giving their beginning? Say I had the following commands in history:
    dir, list, avail, list dates
    and I want to just scroll through the commands which start with "l" letter.
    I tried to enter "l" and then shift and some other ocmbinations but it did
    not work out. Is it possible to filter the other entries out?


    BTW: there is another funny thing: when I run "ed otr.log" then I get a long
    line with "Progress: 1%Progress: 2%" and so on. When I "type otr.log" all I
    get is "Progress: 100%". Seems to be a problem with oneliners as it prints
    files like SYS:Docs/Booting MorphOS.txt correctly.
  • »26.03.13 - 19:10
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    igracki
    Posts: 411 from 2003/2/25
    From: Berlin
    Hi,

    the "problem" with ed or multiview is, the argument to them has to be located in the current directory or should be specified with full path, f.e. C:lha.

    Unfortunately, the backticks can't be nested, so you can't do something like this:
    Code:
    multiview `which `search sys:commands.txt nonum lha``


    Would be nice, if the MorphOS shell would support "$()" as an alternative to the backticks, which would simplify nesting such commands:
    Code:
    multiview $( which $( search sys:commands.txt nonum lha ) )


    Quote:

    That way I have two times the command "lha". Can I make a call to use only
    one time this parameter?


    Hmm, don't know, maybe a "for" command like csh has, could help here, then you could do something like this:
    Code:
    for i ( `search sys:commands.txt file1 nonum` )  "touch $i; filenote $i"


    Quote:

    Ah, and is it also possible to pick up older commands in the Shell history
    by giving their beginning?


    Yes, the builtin shell, has this feature, the hotkey is "shift space"!

    The thing with "otr.log" is, because there are no "linefeeds" after each line, only "carriage returns" which put the cursor to the beginning of the current line and then overwrites it. A "linefeed" writes the text to a new line.

    Its the output of "otrdecoder", so it makes sence there, else you would see many "Progress: " lines.
    BTW, I didn't know that someone else uses my otrMUI;)
    If you like I could send you a updated version which can display 9 thumbnails of an otrkey as a little preview and has some fixes, its still beta, I want to also add cutlist support, so you could search for a cutlist and apply it from otrMUI.
  • »27.03.13 - 19:52
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    connor
    Posts: 570 from 2007/7/29
    igracki,
    Quote:


    Would be nice, if the MorphOS shell would support "$()" as an alternative to the backticks, which would simplify nesting such commands:
    Code:
    multiview $( which $( search sys:commands.txt nonum lha ) )



    Yes, something like that (no matter which notation) is what I would like to use. Just pass arguments from one command to another.
    Quote:


    Quote:

    Hmm, don't know, maybe a "for" command like csh has, could help here, then you could do something like this:
    Code:
    for i ( `search sys:commands.txt file1 nonum` ) "touch $i; filenote $i"



    There is no "for" command in Shell. Is it available in an other place? SDK also does not have it.
    Quote:



    Quote:

    Ah, and is it also possible to pick up older commands in the Shell history
    by giving their beginning?


    Yes, the builtin shell, has this feature, the hotkey is "shift space"!


    Okay, shift + space works but only in one way and it is not intuitive to reach that. Should be placed on more obvious hotkeys and work in both directions.
    Quote:



    The thing with "otr.log" is, because there are no "linefeeds" after each line, only "carriage returns" which put the cursor to the beginning of the current line and then overwrites it. A "linefeed" writes the text to a new line.

    Its the output of "otrdecoder", so it makes sence there, else you would see many "Progress: " lines.



    When I was running otrdecoder there were many progress lines, I remember. So I was surprised that it is only one line as a textfile.
  • »28.03.13 - 18:21
    Profile
  • Fab
  • MorphOS Developer
    Fab
    Posts: 1331 from 2003/6/16
    Quote:


    There is no "for" command in Shell. Is it available in an other place? SDK also does not have it.



    if you use SDK's sh, you can of course use for (as well as everything else).
  • »29.03.13 - 10:11
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    connor
    Posts: 570 from 2007/7/29
    Funny thing: execute not
    Code:

    Run `search SYS:textfile app NONUM`

    but
    Code:

    Run `search SYS:textfile ass NONUM`

    and watch the result.
    Then change to
    Code:

    Run `search SYS:textfile mass NONUM`

    and watch again. What's the problem with 'ass'?
  • »02.04.13 - 18:01
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    igracki
    Posts: 411 from 2003/2/25
    From: Berlin
    Whats the contents of SYS:textfile?
  • »03.04.13 - 13:21
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    connor
    Posts: 570 from 2007/7/29
    It contains names of files like 'massstorage.class', 'Ambient', 'Multiview' etc. So searching for 'ass' should be doable. Or does it have to be from the beginning of the word?

    Code:
  • »04.04.13 - 18:42
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    igracki
    Posts: 411 from 2003/2/25
    From: Berlin
    Yes, searching works, but you cant run every file, only executables!
    Try doing only the search command, to see which line was found.
  • »04.04.13 - 21:52
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    connor
    Posts: 570 from 2007/7/29
    When there is a matching file starting with 'mass' it gets executed. When I run/search for 'ass' it does not get start. It is the same executable. Let's call it 'massive.exe'. And in this case there is only one match. No other hit can come across. So does run/search require a search beginning with the first character of the command to execute what it has found? If yes: why? If not then this is a bug for me.
  • »09.04.13 - 21:00
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    igracki
    Posts: 411 from 2003/2/25
    From: Berlin
    No. "Run" and "Search" are commands for their own.
    If you execute only the search command, and put the result here, maybe I could tell you where is the problem.
  • »11.04.13 - 19:10
    Profile Visit Website