Suppose I have defined an array, like this:
DIR=(A B Supercalifragilistic)
and I need to invoke the script as
./script A B Supercalifragilistic
where the arguments are processed by internal functions func1()
and func2()
. Is there a way to make an alias (or anything, however it's called) S
for Supercalifragilistic
so that when I invoke:
./script A B S
the internal functions will process/interpret S
as Supercalifragilistic
?
Thank you in advance.
[edit]
I should add that the script is invoked via terminal, not inside a script, and the arguments A B Supercalifragilistic
, or (hopefully) S
, are passed on to the script in the terminal. I'm sorry for the confusion.
[edit2]
The script is here: Bash script: if any argument is "N" then function has extra options , in the answer below. What it does is explained in the OP there, below the script. Finally, instead of DIR=(A B C D E F)
it's DIR=(A B Clarification D E F)
(it's just an example) and the folder Clarification
is the only one in a different path than the rest. I hope it's more clear now, if not, please tell me.
[final edit, I hope]
I think I can shout "Evrika!". Your word "hardcoded" made me realize I have to modify the script anytime a new folder gets added/deleted, so I thought of making the array dynamic, as in
./script a b "d e" g
results in array=(a b "d e" g)
but also that it should replace the long paths with some short ones (Clarification >> C
), so I made this test script based on also the answers here:
#!/bin/bash
array=()
for i in "$@"
do
if [[ "$i" == C ]]
then
array+=("Clarification")
else
array+=("$i")
fi
done
echo ${array[*]}
echo
for i in $(seq 0 $(( $# - 1 )))
do
echo ${array["$i"]}
done
and this is what it shows at command prompt:
$ ./x.sh abc C "d f" e
abc Clarification d f e
abc
Clarification
d f
e
I think now I can finally make the script to do what I want. Thank you, all, for the answers.
You can do this:
It'll replace the value "S" with "Supercalifragilistic" anywhere in the array.
I really have no idea what you exactly want to achieve! But I had a look at the script you linked in your last edit. Since you have a hard-coded array you might as well instead use an associative array:
to loop on the keys of
dir_h
, i.e., onA B C D E
:Try it, this might help you with your "alias" problem (or not).
Here's your script from your other post, using this technique and in a more consistent and readable form (note: I haven't tried it, there might be some minor typos, let me know if it's the case):
From this you can do a lot, and have pretty much infinite "alias" handling possibilities.
No need to use an alias. You could try something like :
You don't need alias here. Just set variable
S
to your string:and then use:
OR else call your script directly using array:
PS: It is not a good habit to use all caps variable names in shell and you can accidentally overwrite
PATH
variable some day.