This question already has answers here:
How to pass an array argument to the Bash script
(8 answers)
passing multiple arrays as a parameter in bash
(2 answers)
Closed 3 years ago.
I have a bash script that I need to run for a series of files. The script takes 4 arguments. The first ref is a file, fun is a directory, R1arr and R2arr each are arrays containing a series of files with specific features in a given directory.
My script.sh is as follows:
ref="$1"
fun=$2
R1arr="$#"
R2arr="$#"
analyseReads -i "$ref" -o "$fun" --left "${R1arr[#]}" --right "${R2arr[#]}"
And I run it for various files as follows:
FORWARD=($(for i in Sample_STE*/*R1; do echo $i; done))
REVERSE=($(for i in Sample_STE*/*R2; do echo $i; done))
bash script.sh "$ref" "$fun" "${FORWARD[#]}" "${REVERSE[#]}"
I get an error since "${FORWARD[#]}" and "${REVERSE[#]}" does not only contain the files in the array FORWARD and REVERSE but it also contains "$ref" and "$fun". Why does this happen and how could I solve this issue?
Related
This question already has answers here:
bash command not found when setting a variable
(2 answers)
Closed 2 years ago.
Bash in Linux give me command not found error in Line 9 where I do $array[$i]="Name_Of_File$i".
There are also the correct numbers printed besides the error. But i can't figure how it is possible. Maybe some spaces ? On the net i fuond these sintax of writing assignement with an array.
declare -a array;
start=0;
NumFile=$(ls -1 -d log/log_cassiere* | wc -l);
for (( i=$start; i<$NumFile; i++))
do
$array[$i]="Name_Of_File$i";
done
echo ${array[0]};
The problem with:
$array[$i]="Name_Of_File$i"
is that it is actually interpreting $array[$i] because you have a $ preceding it. That means it will attempt to replace the left side of the assignment with the value. Instead, you should have:
array[$i]="Name_Of_File$i"
This question already has answers here:
Bash array with spaces in elements
(14 answers)
How can I handle an array where elements contain spaces in Bash?
(3 answers)
Closed 2 years ago.
In a script such as this
#! /bin/bash -i
arguments=(arg1 "arg2 has many spaces")
echo arguments count = ${#arguments[#]}
echo arguments = ${arguments[#]}
echo ls ${arguments[#]}
ls ${arguments[#]}
How can I keep arg2 as a single argument to ls?
This is the actual output:
arguments count = 2
arguments = arg1 arg2 has many spaces
ls arg1 arg2 has many spaces
ls: arg1: No such file or directory
ls: arg2: No such file or directory
ls: has: No such file or directory
ls: many: No such file or directory
ls: spaces: No such file or directory
This shows that arguments initially has 2 elements but when it is used as arguments in the ls command the quotes are no longer there (obviously, since they were remove already when arguments was initialized) so ls sees 5 arguments.
How can I apply quoting to each of the array elements separately in order to give ls two arguments by keeping the second element together?
(In the real case I don't know the actual content of the array and ls is only an example which illustrates the problem.)
This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 5 years ago.
I am writing a Bash Shell script, which reads a bunch of lines from a file, and tries to append each line to an array, thus:
# counter variable
i=0
cat doc.txt | while read -r line; do
myArr[i]="$line"
((i=i+1))
done
The file doc.txt contains one word in every line. When I print the array (through echo ${myArr[#]} or for x in ${myArr[#]};do echo $x; done), my array seems to be empty. What am I doing wrong? Thanks!
Edit: I also tried doing this in Ksh, with the same results. An answer in either Bash or Ksh is acceptable, since I seem to be having the same issue in both.
You are running the while loop in a subshell, so any changes you make to the array disappear once that shell exits. Use input redirection instead:
while IFS= read -r line; do
myArr+=("$line")
done < doc.txt
If you using bash 4.0 or later, you can use a single command
readArray -t myArr < doc.txt
This question already has answers here:
Bash array: Unexpected Syntax error [closed]
(3 answers)
Closed 6 years ago.
When I run the following code
#!/bin/bash
folder="~/Desktop/"
if [ -d "$folder" ];
then
cd $folder;
listofpdf=$( find *.pdf -type f);
fi
echo ${listofpdf};
for words in $listofpdf
do
array+=("$words")
done
I get "Syntax error: word unexpected (expecting ")")". It seems to be related to the array variable. Appreciate corrections . Thanks in advance
Although your script is a valid bash script, it is not a valid POSIX shell script: arrays are not supported. Even if sh is a link to or copy of bash, it runs in a strict POSIX mode. In order to run a script with bash extensions, it must be run with bash, either by running bash script or (as you did) using #!/bin/bash as the shebang and executing the script directly with ./script
This question already has answers here:
Calling an executable program using awk
(9 answers)
Closed 9 years ago.
I want to call Code.c from within awk. Note that the Code.c takes current record (i.e. $0) as argument.
I am new to shell scripting so any help would be appreciated.
Best Regards
#!/bin/bash
while read line
do
para=`echo $line | awk '{print $0}'`
./c_code $para
done < your_process_file