Linked Questions
10 questions linked to/from Arrays in Unix Bourne Shell
0
votes
1
answer
4k
views
Trying to find the index of an entry in a list [duplicate]
I am trying to find the index of an list entry using this simple method:
#!/bin/sh
partList="500 1000 2000 4000"
a=( $partList )
echo ${a["500"]}
this returns Syntax error: "(" unexpected. And the ...
20
votes
3
answers
107k
views
sh + how to use array in sh script in order to print all values in array
I want to use arrays in my sh script.
My target is to create an array with the values a b c and print all values in the array.
I succeeded to print each array, but I failed to print all values in ...
20
votes
6
answers
1k
views
Should variables be quoted when executed?
The general rule in shell scripting is that variables should always be quoted unless there is a compelling reason not to. For more details than you probably want to know, have a look at this great Q&...
18
votes
4
answers
46k
views
Printing an array to a file with each element of the array in a new line in bash
I am trying the contents of an array into a file with each element of the array in a new line in the file.
IFS=$'\n'
echo "${mtches[@]}" > sample1.txt
The content of mtches is "qwe" and " asd"....
2
votes
2
answers
2k
views
How to join elements of an array in POSIX delimited by a space?
The following works in a Bash script:
PACKAGES=(
'curl'
'git'
'htop'
'mc'
'tree'
'vim'
)
apt --yes install ${PACKAGES[@]}
But how can I do the same in POSIX so that I can use ...
2
votes
2
answers
1k
views
sh -c not get the right output when execute shell script
I loop to get data from array in shell ,
It works when I execute it in a shell file, with this content:
arr=(1 2 3 4 5)
for var in ${arr[@]};
do
echo $var
done
But there isn't any output ...
1
vote
2
answers
1k
views
Escaping spaces in dash [duplicate]
I want to do something like this.
$ touch a\ b
$ cmd=cat\ a\ b
$ echo $cmd
cat a b
$cmd
cat: a: No such file or directory
cat: b: No such file or directory
There are problems with the spaces in the ...
-2
votes
1
answer
1k
views
How can i make multiple-read? [closed]
I can not give much explanation because my English is bad. The example of what I want to do is below;
This is an example.
try.sh
#!/bin/sh
echo -n "Enter the port:"
read ports
if [ ! -d "$ports" ]; ...
-1
votes
1
answer
435
views
Problem with sh in my shell script
I was going through the shell script book, but in the array section, I got stuck. when I declare the array and run the script like:
sh hello.sh
it shows me the error, even in ZSH, is below:
one.sh: ...
4
votes
1
answer
162
views
"$@" expansion for user defined variables
I'm trying to get a (bourne shell) variable to expand like "$@" so it produces multiple words with some having preserved spaces. I've tried defining the variable in many different ways but still can't ...