![]() |
![]() |
#1 |
Registered User
Join Date: Jun 2020
Location: Germany
Posts: 380
|
Create a (bash) function in a shell script?
Hi, I'm looking for a way to optimize a script I'm currently working on.
It's purpose is to sort files into alphabetical subfolders. Which basically means that I have to use the same code block 26 times for each (normal) letter. I already have a code block which relies on a variable and therefore it does not have to be altered for each letter. In a bash script I simply could turn that block into a function. Which makes the code easier to change (just one block instead of 26) and it would be much smaller. The code block looks like this: Code:
SET Letter "A" LIST >>T:AlphaSort.${Letter} FILES "${Source}" PAT="${Letter}~(.info)" LFORMAT="${Command} *"%P%N*" *"${Target}${Letter}/*"*nECHO *"*E[43m *E[40m*" NOLINE" ECHO "*E[43m *E[40m" NOLINE IF "empty" EQ "`ECHO "*`LIST FILES "T:AlphaSort.${Letter}" LFORMAT="%L"*`" NOLINE`" DELETE >NIL: "T:AlphaSort.${Letter}" ELSE ECHO >>T:AlphaSort.EXE "ECHO *"${Action}: ${Letter}#? to ${Target}${Letter}/... *" NOLINE" ECHO >>T:AlphaSort.EXE "MAKEDIR >NIL: ${Target}${Letter}" TYPE T:AlphaSort.${Letter} >>T:AlphaSort.EXE ECHO >>T:AlphaSort.EXE "ECHO *" Done.*"" ENDIF I'll attach the current version of the script, so you can check it out completely. I added the .s extension for it to be a valid file but it is just a normal text based shell script. Edit: Removed this version. See below for newer version. Last edited by EctoOne; 18 March 2023 at 13:03. |
![]() |
![]() |
#2 |
Registered User
Join Date: Jan 2002
Location: Germany
Posts: 7,047
|
You could walk through the alphabet like this:
Code:
set num 1 lab loop set letter `echo "ABCDEFGHIJKLMNOPQRSTUVWXYZ" first $num len 1` echo ${letter} set num `eval $num + 1` if val $num not gt 26 skip back loop endif |
![]() |
![]() |
#3 |
Registered User
Join Date: Jun 2020
Location: Germany
Posts: 380
|
Thanks again thomas, it works great.
As for going with ARexx, yeah I really can't get into it. Compared to (bash) scripts I can't easily determine what is doing what or where something starts/ends/is defined or whatever. My default way of learning things has always been that I took an available piece of code that kind of does what I want to do and then delete/change things until I'm happy. And from there I can adapt for feature projects. But for ARexx it feels like I have to start from scratch with the usual "Hello world" thing, and I just can't. ![]() Edit: Added updated version if anyone wants to use it. Edit 2: Updated version attached. Last edited by EctoOne; 19 March 2023 at 15:55. |
![]() |
![]() |
#4 |
Registered User
Join Date: Oct 2009
Location: Germany
Posts: 3,325
|
ARexx is rather easy to learn. Especially if you have already some coding/scripting skills. To start learning you just need (or want) the Amiga ARexx book and ARexxGuide2 from Aminet (IMO). What you describe as your way of learning seems to be a dead end.
ARexx has a much better syntax then Amiga DOS. Your script above shows how "readable" it is. ![]() "Hello World" is as easy as "echo 'Hello World'" in Amiga DOS. The first thing you need to know that each ARexx script starts with a comment: Code:
/* Hello Word script */ ECHO "Hello Word" SAY "Is that easy?" /* Because we like it short */ a = 10 b = 4 c = a + b SAY c SAY a - b IF a = 10 THEN DO SAY c - b END |
![]() |
![]() |
#5 |
Registered User
Join Date: Jun 2020
Location: Germany
Posts: 380
|
Well, we are all different, aren't we?
I'm not saying my way of learning is perfect but it is good enough for me. I was always more the practical guy who took something apart just to see how it works. Sometimes I wasn't able to put it back together but thankfully that is not that big of a issue when it comes to scripting. I get bored very fast when I try to learn something new. Especially when I do it step by step. And I actually looked at the ARexxGuide2 before because I was interested to see what it is capable of. Another "problem" is that there are also a few extra libraries to expand the possibilitys of ARexx, and that is something that discouraged me. I don't want to search and add functions if needed. I also think that I like the challenge of trying to see what is possible with the minimum requirements. As for the readability, when I looked at ARexx scripts. I had a hard time recognizing what is a command, or what is a variable or an argument. Because there are so few indicators like quotes, brackets or whatever. Anyway, you don't want to try to understand my script, I gave up on learning ARexx. ![]() |
![]() |
![]() |
#6 |
Registered User
Join Date: Oct 2009
Location: Germany
Posts: 3,325
|
If you learn by looking at scripts/code from others it is fine of course (takes much time and is difficult), but when you notice you have problems with understanding then you have to change or give up (as you wrote). I.e. you can only learn easy simple stuff.
And you are not forced to use libraries or search or add functions if needed. Just learn the basics. Amiga DOS or ARexx. The books come with several examples which are much better/easier to understand. Foreign code is always difficult to read. Moreover you will have problems to read you own code that you haven't touched for a long time. |
![]() |
![]() |
#7 |
Registered User
Join Date: Jun 2020
Location: Germany
Posts: 380
|
Lets agree to disagree ok?
If you can learn something from the ground up, good for you. I can't and I'm still happy with it. Would it be nice to create something bigger? I don't know. Maybe, but I don't care. I'm fine with easy simple stuff, especially since I usually do it just for myself and don't release it like in this case. And so far I never had any issues going back to old code, might be my formatting or that I use some sort of method to comment the blocks. Might be not as obvious in this case, but it is there. |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Clear background color for Shell window- Shell-StartUp | fc.studio | support.Apps | 13 | 25 March 2022 18:52 |
fd2pragma inline entry for function that returns pointer to another function | Sim085 | Coders. C/C++ | 11 | 02 March 2022 10:38 |
Start script without opening a Shell window | Joel_w | support.Apps | 6 | 01 February 2019 16:56 |
A script to create a bootable floppy from Blitz Basic 2 iso | TenLeftFingers | support.Apps | 17 | 20 November 2016 21:09 |
How to make bash behave like KingCON shell? | rsn8887 | support.Other | 3 | 31 July 2007 03:04 |
|
|