This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Copying a Bash array fails
(2 answers)
Closed 3 years ago.
I'm new to shell script and am having an issue with the shuf function.
This is my code
declare -a myarray=( 'A' 'B' 'C' 'D' 'E' 'F' )
myarray = $(shuf -e "${myarray[#]}")
echo "$myarray"
I make an array containing the six characters. I then shuffle them randomly, and print them out. My issue is that if I were to add another line, for example
echo ${myarray[2]}
This doesn't actually print the randomly sorted character in the 3rd position. Instead, it will always print 'C'. How can I actually save the sorted array? Do I need to make another array?
Thank you very much
Arrays in bash are defined with (). Bash is not statically typed, so setting myarray equal to some output of characters will do just that, making it a string you can echo with echo $myarray to see the full output.
You need to wrap your output in parens to make it clear to bash that your new myarray should also be an array:
myarray=($(shuf -e "${myarray[#]}"))
Related
This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed 2 years ago.
I have a string like this:
line="06:13:41.817 INFO ProgressMeter - Traversal complete. Processed 46942 total variants in 2.2 minutes."
I want to extract 46942 and put in a variable.
I tried this, but it only removes the Processed and total. How do I do it?
Var=$(echo $line | sed -e 's/Processed\(.*\)total/\1/')
Use parameter expansion:
line="06:13:41.817 INFO ProgressMeter - Traversal complete. Processed 46942 total variants in 2.2 minutes."
line=${line#*'Processed '} # Remove from left up to "Processed ".
line=${line%' total'*} # Remove from from " total" up to the end.
This question already has answers here:
Creating an array from a text file in Bash
(7 answers)
Closed 2 years ago.
I'm trying to store each word from a file (named f1.txt) in an array (in bash), and then I want to print each element of the array. I tried something like this:
n=0
for varWord in $(cat f1.txt);
do
word[$n]=$varWord
$((n++))
done
for((i=0;i<n;i++));
do
echo $((word[i]))
done
I've also tried this (In fact, it was my first approach, as I would also prefer not to use an additional variable -- varWord, like I did above):
n=0
for word[$n] in $(cat f1.txt);
do
$((n++))
done
for((i=0;i<n;i++));
do
echo $((word[i]))
done
read -r -d '' -a words <inputfile.txt
That's all it needs.
This question already has answers here:
Read n lines at a time using Bash
(17 answers)
Closed 3 years ago.
How do I read every 100 lines in a larger file of around 100000 lines. I want to read every 100 lines in one iteration and make them coma seperated, run some code and next iteration, it should pick from 101 to 200.
I have searched internet, everywhere there is a solution for picking nth line and not n lines.
You can use mapfile and specify count. Remember to check the end condition, as mapfile doesn't set it's return status to nonzero on EOF condition.
# read 100 lines
while while mapfile -t -n2 lines && ((${#lines[#]})); do
# output the lines separated with comma
( IFS=','; echo "${lines[*]}"; )
# run some code
: run some code
done < larger_file
This question already has answers here:
How to reverse a list of words in a shell string?
(18 answers)
Closed 3 years ago.
I'm trying to write a bash script to swap the words entered by the user.
For example: hello stack overflow
Output: overflow stack hello
User can enter any number of words.
try this:
read -ra line # read into array
i=${#line[#]} # determine length of array
for i in $(seq $((i-1)) -1 0); do # loop in reverse order
echo -n "${line[i]} " # echo entry at i-position without linefeed
done
echo # linefeed
input
this is a test
output
test a is this
Take a look at the following article explaining the use of "read" in bash:
http://landoflinux.com/linux_bash_scripting_read.html
If you know ahead of time the number of words you will be switching, a simple solution could be something like this. Each word is assigned to a variable specified in your read command:
#!/bin/bash
echo "Enter two words: "
read one two
echo "first word: $one"
echo "second word: $two"
If you need to reverse a list of words in one string, you can take a look at this answer:
How to reverse a list of words in a shell string?
This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 4 years ago.
I have a list of files to put through a for loop. They are named
FA2_00032.png, FA2_00033.png, etc
I have variables $imID which contains FA2 string, $startFrame which contains the start frame (e.g. 00034) and $endFrame which contains to end frame (e.g. 00894).
I have managed to get the list of relevant files using:
eval echo ${imageID}_{${startFrame}..${endFrame}}.png;
This outputs
FA2_00034 FA2_00035 FA2_00036 etc
But now I need to pass this to the for loop. That is,
for file in *the above list*
where *the above list* is the block quoted list above. $file should contain FA2_00034, FA2_00035, etc for use in the for loop.
Use a C-style for loop, not eval+{...}, for tasks like this. printf can be used to assemble the file name from the current integer. The syntax 10#$foo ensures that the value of $foo is treated as a decimal (not an octal) number if it includes leading 0s.
imageID=FA2
startFrame=00034
endFrame=00894
for ((i=10#startFrame; i<=10#$endFrame; i++)); do
printf -v file '%s_%05d.png' "$imageID" "$i"
echo "Operating on $file" >&2
done