Shell script - iterate over space separated words/characters (in zsh) - shell

I am having some trouble figuring out how to iterate over space separated words/characters in a shell script. For instance I would like to iterate over a variable containing the characters in the alphabet separated by a space.
NOTE: The result should be the same even if the alphabet variable contained space separated strings instead of characters, i.e "aa bb cc ..." instead of "a b c .."
I have tried a lot of the alternatives provided from:
How to split a line into words separated by one or more spaces in bash?
Example:
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in $alphabet; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
Expected/Desired output:
1. a
2. b
3. c
and so on..
Result:
1. a b c d e f g h i j k l m n o p q r s t u v w x y z
Additional tests(without success):
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in ${alphabet[#]}; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local alphabetArray=( ${alphabet} )
local index="0"
for character in "${alphabetArray[#]}"; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local alphabetArray=( ${alphabet} )
local index="0"
for character in ${alphabetArray}; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
Could someone provide a solution on how to solve this(I would prefer a solution that iterates the alphabet variable without explicitly using an index variable, i.e $alphabet[index] )?

Thanks for your help. I discovered the error thanks to your feedback.
I thought that it was irrelevant when I posted this question but I was experimenting with functions in my .zshrc file. Hence I was using (just my assumption) the zsh interpreter and not the sh or bash interpreter.
By realizing that this could be a potential problem, I googled and found the following How to iterate through string one word at a time in zsh
So I tested the following and it works as expected:
setopt shwordsplit
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in $alphabet; do
index=$(($index+1))
echo "$index. $character"
# Possibility to do some more stuff
done
unsetopt shwordsplit
NOTE:
index=$((++$index))
and/or
index=$(($index++))
Doesn't seem to work as I expected in zsh.
... The little gritty details, I should have used:
((++index))
or
((index++))
instead of
index=$((++$index))

Try this
IFS=$' \t\n'
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in $alphabet; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
Hope it helps

Related

Can I use a GraphQL union for plain strings?

In Graphql, I can create a union such as the following:
union SearchResult = Book | Movie
Is there a way I can do this for plain strings? Something like this:
union AccountRole = "admin" | "consumer"
I am afraid you cannot do that because it is what defined by the specification.
From the union syntax mentioned at specification here , the part that you want to change should follow the Names syntax , which the first character is only allow to be upper case letter, lower case latter or _
(i.e. the characters set as follows)
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m
n o p q r s t u v w x y z _

What are those elements in the method?

This is an extract of a file from this project (full text here):
Method redirects() [ Private, ProcedureBlock = 0 ]
{
/// Mnemonics
APC d APC^%X364 q
BEL d BEL^%X364 q
CBT(%1) d CBT^%X364(%1) q
CCH d CCH^%X364 q
CHA(%1) d CHA^%X364(%1) q
CHT(%1) d CHT^%X364(%1) q
CNL(%1) d CNL^%X364(%1) q
CPL(%1) d CPL^%X364(%1) q
CPR d CPR^%X364 q
CTC(%1,%2,%3,%4,%5,%6,%7,%8,%9) d CTC^%X364(%1,%2,%3,%4,%5,%6,%7,%8,%9) q
CUB(%1) d CUB^%X364(%1) q
CUD(%1) d CUD^%X364(%1) q
CUF(%1) d CUF^%X364(%1) q
CUP(%2,%1) d CUP^%X364(%2,%1) q
CUU(%1) d CUU^%X364(%1) q
CVT(%1) d CVT^%X364(%1) q
DA d DA^%X364 q
DAQ(%1,%2,%3,%4,%5,%6,%7,%8,%9) d DAQ^%X364(%1,%2,%3,%4,%5,%6,%7,%8,%9) q
DCH(%1) d DCH^%X364(%1) q
DCS d DCS^%X364 q
DL(%1) d DL^%X364(%1) q
DMI d DMI^%X364 q
DSR(%1) d DSR^%X364(%1) q
EA(%1) d EA^%X364(%1) q
ECH(%1) d ECH^%X364(%1) q
ED(%1) d ED^%X364(%1) q
EF(%1) d EF^%X364(%1) q
EL(%1) d EL^%X364(%1) q
EMI d EMI^%X364 q
EPA d EPA^%X364 q
ESA d ESA^%X364 q
FNT d DNT^%X364 q
GSM d GSM^%X364 q
GSS d GSS^%X364 q
HPA(%1) d HPA^%X364(%1) q
HPR(%1) d HPR^%X364(%1) q
HTJ d HTJ^%X364 q
HTS d HTS^%X364 q
HVP(%1,%2) d HVP^%X364(%1,%2) q
ICH(%1) d ICH^%X364(%1) q
IL(%1) d IL^%X364(%1) q
IND d IND^%X364 q
// And others, followed by old style MAC routines
}
This is the first time I see that... And I can't find documentation on what those "mnemonics" are.
What are they? Where is the documentation for it?
This is a standard mnemonics implementation for WebTerminal inside WebTerminal itself.
To make WebTerminal work as common terminal do over WebSockets, one of the most important things is a little line of code
use $io:(/NOXY:/BREAK):"^" _ ..InitialZName
which is executed at the beginning of WebSocket server initialization, and which actually set up the name of the mnemonic space, which is equal to WebTerminal's compiled routine name (like WebTerminal.Engine.1.int).
These mnemonics (APC, BEL, etc) are a little macro programs, which user can call from the terminal using the special syntax. For example, mnemonic "CHA" is used to set the caret position:
USER > w "Pos 0", /CHA(14), "Pos 14", /CHA(35), "Pos 35"
Pos 0 Pos 14 Pos 35
In order to make user able to access all of these with this syntax, there were a need to include all standard mnemonics names into terminal routine, which is set as a mnemonic space for each client by default (because setting the default mnemonic space, which include these mnemonics breaks WebTerminal). The only solution left is to declare them inside any method (in this case, the method named "redirects") using ProcedureBlock = 0.
Talking about the syntax, in general, we have
MNEMONICNAME(%ArgByRef) do MNEMONICNAME^%SYSTEMROUTINE(%ArgByRef) quit
This just calls all of the standard mnemonics which can be found in ^%X364 system routine.

Capitalize first letter of a filename

I have some files in a folder and would like to uppercase the first letter of all the filenames with a certain extension using a batch script in windows.
example cap only *.m
before:
foo.m
bar.m
picture.jpg
after:
Foo.m
Bar.m
picture.jpg
for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
ren %%a*.m %%a* >nul 2>&1
)
Check also this -> https://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards

How to delete the last space at the end of each line in a text file? (shell scripts)

I have a file like this:
z E l f
A l t E^ t
d Y s
m u s t
z E l f s
x # w e s t
s t e t s
h E p
w i
t E n
o G #
o G # n
m I s x i n
s t O n t
and I need to remove a space at the end of each line.
How can I do it? Thank you in advance.
I assume you want to delete trailing spaces and tabs from the end of each line.
awk '{ sub(/[ \t]+$/, ""); print }' file

how do I select words with a particular structure from a file in a shell script?

My file has this structure:
HELLO h e l lo
HELLO(2) h ee l lo
HELLOMUM h e l ll m um
HELLO-FATHER h e llo fa th er
HOME h o m e
HONEY h o ne y
HONEY(2) ho nei y
HONEY(3) h o ney
HONEYMOON ho ney m o o n
HONEY-MOON h o ne y moo n
I would like, while looking for the word HELLO, for example, to extract only the lines relatives to the words HELLO and HELLO(2). Same thing if I am looking for the word HONEY, I would like to have only the lines relative to HONEY, HONEY(2) and HONEY(3).
I am using
grep -w "HELLO" file.txt
but this gives me back for HELLO, for example, also the lines relative to HELLOMUM and HELLO-FATHER.
Thank you in advance.
I guess this should work for you.
grep '^HELLO[^a-zA-Z]' your_file
tested below:
> grep '^HELLO[^a-zA-Z\-]' temp
HELLO h e l lo
HELLO(2) h ee l lo
> grep '^HONEY[^a-zA-Z\-]' temp
HONEY h o ne y
HONEY(2) ho nei y
HONEY(3) h o ney
>
take your pasted example as file, the following grep line works:
grep 'HELLO[( ]' file
grep 'HONEY[( ]' file

Resources