Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
So a user can input this in the bash script when ask: 1,2,3,4,5,6 comma separated.
Now what I wanted is to append and repeat it with the string so the results would be like:
hi1 hi2 hi3 hi4 hi5 hi6
This works with:
"hi"{1,2,3,4,5,6}
The problem is using the user input to the loop to be used as the parameter to it. I tried using this but it does not work.
"hi"{$USERINPUT}
I do not have deep experience with bash to know this part.
Using bash pattern substitution and printf:
printf "hi%s " ${USERINPUT//,/ }
printf does not require a loop and prints as many strings as there are arguments.
The bash substitution is ${parameter/pattern/string} which is documented in the bash man page.
How about using sed?
$ echo "$USERINPUT" | sed 's/[0-9][0-9]*/hi&/g;s/,/ /g'
hi1 hi2 hi3 hi4 hi5 hi6
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have a C program which reads its input from the command line, I would like to feed the executable with the output of ls | wc -m command, as I need to call two instances of the executable (./a.out1 , ./a.out2) using that same input and make them running in parallel (pipes ?).
Thank you for your help in advance!
You say you want to use a pipe, so first of all you need to adapt your program to read the input from stdin instead of argc and argv. Input passed via a pipe is not added to the command line argument list.
To pipe stdout of a process to multiple other processes, you can use tee and process substitutions:
ls | wc -m | tee >(./a.out1) >(./a.out2) >/dev/null
However, the reason why you require it be piped (as opposed to passed as an argument) isn't clear to me, so storing the output in a variable as suggested in the comments would work just as well for the example you present.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a script that writes about 20 numbers line by line to a file during processing.
When the script starts again, it reads from this file with this code
declare -a sedum
i=0
while read -r line
do
sedum[$i]=$line
i=$(( $i + 1 ))
done < $f_sday
f_sday contains the filename. When I call the script from comand line it always works fine and reads the complete content of the file.
But when the script is called in a cronjob it reads only two or three values
I know that from cron it might not be the same environment but I can't see any environmental dependency here.
I tried mapfile at first, but that read only two of the twenty values.
Any idea what I am missing here?
Stupid me.
I did not control the working path (cron starts in $HOME), so the script was working on the wrong file.
Thanks for the set -x hint. That led me on the right path!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to create a bash script that uses info pulled from Wikipedia via curl to help sort my music collection. I've gotten it to reliably return the information I want, but because of Wikipedia's formatting, there is sometimes information I want to discard. It is also not always formatted consistently; sometimes it is on multiple lines, and sometimes only one, but the information I want is consistently delimited between "[[" and "]]". I want to keep only the text between [[ and ]] and ignore the rest. All of the solutions I've found so far use sed and rely on consistent formatting. Basically what I want to do is take a long string formatted:
{{[[abcd]]efgh[[hijk]]lmno
[[pqrs]]
[[tuvw]]yz}}
and create an array with the values
abcd
hijk
pqrs
tuvw
With GNU grep and a Perl-compatible regular expression (PCRE):
grep -Po '(?<=\[\[).*?(?=]])' file
Output:
abcd
hijk
pqrs
tuvw
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I made a program which takes numbers from the command line and adds them, the program is supposed to print an error if something else than a number is written, but when it reads > or < it creates text files with the results or rewrites on an already existing text file, if it doesn't exist it just stops without even running the code, is there a way to stop this from happening and read it just like another array?
Here is an example of the error
$ ./a +24 < 5
bash: 5: no such file or directory
That's not your program - that's the shell file I/O redirection. If you want your program to see the < or >, escape them appropriately:
./a +24 \<5
As ths others have said, it is not your program acting up, but the command line shell uses < and > as input/output redirection operators. You could escape them with backslashes.
But rather than forcing the users to escape each < and > (and possibly some other special characters like $ and the parentheses), you can quote the whole command line:
./a '1 < 24'
The whole command now is in argv[1]. You still have to parse it. As a bonus, there is no need to insert annoying spaces between the tokens anymore:
./a '1<24'
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have Linux Ubuntu, and I want to test my program, for which someone gave me .txt file of multiple inputs. Now I want to run this program with inputs written in the .txt file. Theres a lot of inputs so I dont want to input them by hand. Is there some command in Linux Terminal to run a C with inputs written in a file?
thank you for your answers
I think you are suffering from the all too common misunderstanding that "standard input" == "a keyboard". Stop thinking that. If you've already written a program that reads from stdin, all you need to do is associate your text file with stdin. In the shell, you do that with a redirection operator:
./a.out < input.txt
If you have multiple inputs, you can easily invoke your program on each individually:
for file in *.txt; do
echo "Running on input: $file"
./a.out < "$file"
done
or you can run your program once on all the inputs:
cat *.txt | ./a.out
There are many, many ways to do what you want, and a lot of flexibility to do different things. You'll probably want to compare the output of your program with the expected output and then you're on your way to writing a full-fledged test suite. For example:
if ! ./a.out < input.txt | cmp expected-output -; then
echo "TEST FAILED" >&2
exit 1
fi