English Amiga Board


Go Back   English Amiga Board > Coders > Coders. System > Coders. Scripting

 
 
Thread Tools
Old 27 May 2020, 12:13   #1
mc68060
With MMU and FPU!
 
Join Date: Mar 2015
Location: On your mainboard
Posts: 270
Read file and compare it to a string

I want to compare the contents of a file against a string in an AmigaDOS script. Let's say I want to check if the file ram:test contains the string "Hello World". I've tried to do it like this but it doesn't work:

Code:
echo >ram:test "Hello World"
set foo <ram:test
if $foo eq "Hello World"
  echo "Yep!"
else
  echo "Nope!"
endif
This prints "Nope!" although I'd expect it to print "Yep!" but I don't understand why it doesn't work...
mc68060 is offline  
Old 27 May 2020, 12:23   #2
deimos
It's coming back!
 
deimos's Avatar
 
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
Carriage return from the echo? Can you add an echo $foo to confirm?
deimos is offline  
Old 27 May 2020, 12:32   #3
Dan
Registered User
 
Dan's Avatar
 
Join Date: Nov 2004
Location: Germany
Posts: 629
Try echo $foo to see what it contains ?!


(edit while i was testing it on winuae, someone answered XD)
Dan is offline  
Old 27 May 2020, 12:37   #4
mc68060
With MMU and FPU!
 
Join Date: Mar 2015
Location: On your mainboard
Posts: 270
WTF... $foo contains "<ram:test" so redirection doesn't seem to work at all Is there any other way?
mc68060 is offline  
Old 27 May 2020, 12:49   #5
Dan
Registered User
 
Dan's Avatar
 
Join Date: Nov 2004
Location: Germany
Posts: 629
what if you save the file as, well use this:

echo >ram:test set foo Hello World

then you do

execute ram:test
echo $foo

it works if you do it in the cli, i'm not sure if it works in/as the batch file.

Last edited by Dan; 27 May 2020 at 13:02.
Dan is offline  
Old 27 May 2020, 13:02   #6
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,985
Quote:
Originally Posted by mc68060 View Post
WTF... $foo contains "<ram:test" so redirection doesn't seem to work at all Is there any other way?
Proper redirection in AmigaDOS always was in the beginning of the command line, just after the command name.

So

set <ram:test foo

should work.

Only since Kick 2.0 redirection is allowed anywhere in the command line, but only if the command does not have a /f argument which takes everything until the end of the line.
thomas is offline  
Old 27 May 2020, 13:02   #7
mc68060
With MMU and FPU!
 
Join Date: Mar 2015
Location: On your mainboard
Posts: 270
Quote:
what if you save the file as, well use this:

echo >ram:test set foo Hello World

then you do

execute ram:test
echo $foo

it works if you do it in the cli, not sure if it works in as the batch file.
But that doesn't solve the problem. I somehow need to read the contents of the file ram:test into a variable so that I compare it against the string.
mc68060 is offline  
Old 27 May 2020, 13:04   #8
mc68060
With MMU and FPU!
 
Join Date: Mar 2015
Location: On your mainboard
Posts: 270
Quote:
Originally Posted by thomas View Post
Proper redirection in AmigaDOS always was in the beginning of the command line, just after the command name.

So

set <ram:test foo

should work.

Only since Kick 2.0 redirection is allowed anywhere in the command line, but only if the command does not have a /f argument which takes everything until the end of the line.
Still doesn't work. Here is the test script:

Code:
echo >ram:test "Hello World"
set <ram:test foo
echo $foo
if $foo eq "Hello World"
  echo "OK!"
else
  echo "Nope!"
endif
echo $foo prints "$foo" now...
mc68060 is offline  
Old 27 May 2020, 13:05   #9
Dan
Registered User
 
Dan's Avatar
 
Join Date: Nov 2004
Location: Germany
Posts: 629
I see, i assumed you are writing this text file, like in your first example, from the batch file.
Dan is offline  
Old 27 May 2020, 13:06   #10
mc68060
With MMU and FPU!
 
Join Date: Mar 2015
Location: On your mainboard
Posts: 270
Quote:
Originally Posted by Dan View Post
I see, i assumed you are writing this text file, like in your first example, from the batch file.
No, that's just for testing purposes.
mc68060 is offline  
Old 27 May 2020, 13:09   #11
Dan
Registered User
 
Dan's Avatar
 
Join Date: Nov 2004
Location: Germany
Posts: 629
this textfile, which you want to check, does it contain only 1 line of text ?


how about this ? https://wiki.amigaos.net/wiki/AmigaO...ference#SEARCH
Dan is offline  
Old 27 May 2020, 13:18   #12
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,985
Quote:
Originally Posted by mc68060 View Post
Still doesn't work. Here is the test script:
You forgot one important thing: set reads the value from the command line, it does not read from the input stream. Therefore redirecting the input stream does not affect it.

There is a trick, though:

set >nil: <ram:test foo ?

The ? makes the command print its template and wait for input. This way you can read from the redirected input stream.
thomas is offline  
Old 27 May 2020, 13:21   #13
mc68060
With MMU and FPU!
 
Join Date: Mar 2015
Location: On your mainboard
Posts: 270
Cool, that's a nifty trick! You are my hero @thomas!

So here is the final script (I also had to add quotes around "$foo" to make it work):

Code:
echo >ram:test "Hello World"
set >nil: <ram:test foo ?
if "$foo" eq "Hello World"
  echo "OK!"
else
  echo "Nope!"
endif
This works perfectly! Thanks a lot!
mc68060 is offline  
Old 27 May 2020, 16:19   #14
jotd
This cat is no more
 
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,162
AFAIR you can also use PIPE: to avoid creating a file.

But once you tried modern scripting languages, Amiga csh is just frustrating.
jotd is offline  
Old 27 May 2020, 16:55   #15
daxb
Registered User
 
Join Date: Oct 2009
Location: Germany
Posts: 3,303
I would say if you are not forced to use AmigaDOS then don't use it.
daxb is offline  
Old 29 May 2020, 05:51   #16
Hewitson
Registered User
 
Hewitson's Avatar
 
Join Date: Feb 2007
Location: Melbourne, Australia
Age: 41
Posts: 3,772
Would be nice to have a modern version of zsh for Amiga. The latest version on Aminet is from 1996.
Hewitson is offline  
Old 08 September 2021, 19:03   #17
carls
Registered User
 
Join Date: Apr 2012
Location: Cyberspace
Posts: 57
You could also use backticks:

echo noline "Hello World" > ram:test
set foo=`type ram:test`
carls is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Disk \ ADF Compare felixkat request.Apps 46 28 March 2021 00:00
[blitz] how to set initial variables read from a file peceha Coders. Blitz Basic 3 20 September 2019 11:56
overscan and mouse settings option to be read from external file _psy project.WHDLoad 5 15 April 2015 13:30
What version of DX did the AGA compare to? methodman Retrogaming General Discussion 8 22 May 2010 09:02

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 05:36.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.08794 seconds with 13 queries