Bash: different behaviour reading from file interactive/cron [closed] - arrays

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!

Related

How to fix 'Bad file descriptor' after implementing here-string redirection in my own shell [closed]

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 3 years ago.
Improve this question
I'm working on my own shell bit by bit and arrived at implementing I/O redirection functionalities. I've finished implementing >, < and >>. I'm stuck at implementing <<<.
My tried implementation for <<< is quite simple conceptually. Make a file named herestring, write the string in the file, set the file descriptor as STDIN_IN, execute the command and later delete the file.
The file's purpose is to basically store the string.
The file is being created and deleted successfully. Even the writing of the string inside the file is working. However when I execute the following in my own shell:
cat <<< Hello,world!
Actual results:
cat: -: Bad file descriptor
Expected:
Hello,world!
It's really weird because I've basically re-used the implementation of the input redirection (command < file) and the only part that I added is the part that works. So I'm really at loss what I'm missing in my code.
If more code is needed let me know.
Acording to man creat, a call to creat() is equivalent to calling open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC.
O_CREAT and O_TRUNC make sense in your case. O_WRONLY not so much.
I recommend to use open() with flags O_CREAT|O_RDWR|O_TRUNC
Other issues with the code you have shown: (This is not a complete list!!)
No error checking on 'creat()' 'write()' and 'dup2()' !!
You forgot to rewind your file descriptor
You forgot to close fd
Saving the here-string to a named file is recipe for problems with race conditions,
and may leave clutter in the file-system.
If you intend to save the here-string to a file, you should study the O_TMPFLAG on how to make anonymous files in the file system. Then you don't need to call remove(), the file will automatically be deleted when all its file-descriptors are closed.

How to repeat string using the array of input [closed]

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

Ignore the '<' and '>' in the comand line in c [closed]

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'

How to test a C program with multiple inputs? [closed]

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

Unix cat command with output redirection metacharacter >, create empty files [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I am using following code to create a new file cat15 using cat command in UNIX
# cat > cat15
this command adds a new file cat15 in root directory and whatever I type after this command is being stored into the file created. But I am not able to exit from this editor.
In other word, I am not getting Shell prompt symbol #
The cat command reads from STDIN if you don't specify a filename. It continues to do this until it receives an EOF or is killed. You can send an EOF and get your terminal back by typing <ctrl>+d.
What people generally do is to either use
touch filename
or
echo -n > filename
to create an empty file. As Charles correctly notes below, "echo -n" is not always a good idea (though you can usually count on it under "popular" Linux distros); I'd strongly suggest just using touch.
If you just want to create an empty file, regardless of whether one existed or not, you can just use ">" like this:
> cat15
It will clobber anything that already exists by that name.

Resources