Linked Questions

196
votes
4answers
38k views

Why is using a shell loop to process text considered bad practice?

Is using a while loop to process text generally considered bad practice in POSIX shells? As Stéphane Chazelas pointed out, some of the reasons for not using shell loop are conceptual, reliability, ...
21
votes
3answers
4k views

What is the fastest way to run a script?

I was wondering what is the fastest way to run a script , I've been reading that there is a difference in speed between showing the output of the script on the terminal, redirecting it to a file or ...
11
votes
2answers
3k views

How do I shift a bash array at some index in the middle?

1 #!/bin/bash 2 # query2.sh 3 4 numbers=(53 8 12 9 784 69 8 7 1) 5 i=4 6 7 echo ${numbers[@]} # <--- this echoes "53 8 12 9 784 69 8 7 1" to stdout. 8 echo ${numbers[i]} # <--- this echoes ...
2
votes
4answers
2k views

Can I use comparison operators in case?

I'm building a function that will calculate the gauge of wire required given amperage, distance(in feet), and allowable voltage drop. I can calculate the "circular mils" given those values and with ...
1
vote
1answer
6k views

Bash script inheritance? Calling a function from another script?

I have this line: trap 'jobs -p | xargs kill -9' SIGINT SIGTERM EXIT it is repeated in many bash shell scripts I have. What is the best way to share this code? Can I call a bash function perhaps? ...
7
votes
2answers
1k views

What is the overhead of using subshells?

Hopefully this question is not too generic. I am very new to shell scripting and I come from a computer architecture/non-scripting programming background. I have noticed on the scripts at my work that ...
3
votes
1answer
670 views

bash: preallocate memory for array

Can memory be preallocated in bash for arrays of a defined size? I am iteratively adding strings to an array (declared by declare -a arr) in a loop (arr+=("$str")), and am wondering whether ...
0
votes
3answers
728 views

Using AWK To Extract Numbers From .CSV File

I have a .CSV file that I need to extract numbers from. Example of file: #File of numbers 1,2,3,4,5 6,7,8,9,10 I want to use awk (or another utility) to extract the first number and second number ...
1
vote
1answer
554 views

For with read value

In what way does the final value of number being assigned by read var number (and we enter 5) and number=5 differ? I was making this for loop: read var number #number=5 factorial=1 i=1 for i in `seq ...
0
votes
2answers
797 views

Insert variable in variable line using sed or awk [closed]

i have two variables (txt and a line number) i want to insert my txt in the x line card=$(shuf -n1 shuffle.txt) i=$(shuf -i1-52 -n1) 'card' is my txt : a card randomly selected in a shuffle 'deck' ...
0
votes
1answer
172 views

Comparing files in awk/bash

I came across a problem and have no idea how to find the OPTIMAL solution. I have a list of files that looks like this: file1\0file2\0...fileX\0\0file(x+1)\0 Every name of file is separated by a \0 ...
0
votes
1answer
78 views

How do I write a word, letter by letter into my program's memory

I'm pretty new to unix and I'm wondering how I would go about writing a word, letter by letter into my program's memory. So for example if my word was "cup", I'd want an array A to have A[0] = 'c', A[...