linux bash redirection with stdin and stdout the same file [duplicate] - linux

This question already has answers here:
How can I use a file in a command and redirect output to the same file without truncating it?
(14 answers)
Closed 8 years ago.
i have a problem about bash, command is like:
echo helloworld > hello.txt
cat <hello.txt >hello.txt
at first, there are stuff in hello.txt. what i expect is that it should seem nothing happens to hello.txt after executing the command, but there is nothing in hello.txt then.
is it a mechanism of bash, or is there something i did not understand about linux file descripor?
maybe bash establish only one fd for a certain file? am i right?
can you help me?
/br
ruan

Use a temporary file
cat hello.txt > /var/tmp/tmpFile
mv -f /var/tmp/tmpFile hello.txt
cat hello.txt

Related

how to copy the bash file itself to the output dir [duplicate]

This question already has answers here:
How do I know the script file name in a Bash script?
(25 answers)
Closed 2 years ago.
I want to save a copy of the bash file each time I run it. It should be saved to the output dictionary.
I am doing it like this in the mytrainrtest.sh file:
mkdir -p "${EXP_DIR}/train"
cp "${WORK_DIR}"/mytrainrtest.sh "${EXP_DIR}"/.
Now I have much more bash files with name my****** as copies of the upper one, each with different names.
How can I write the line, so the bash file will recognize its name to copy itself?
Use the $0 special variable which contains the name of the currently executing script.
cp "$0" "$exp_dir"/
Script name(path) stored in a special var $0
#!/bin/bash
echo $0
$ ./test
./test

Bash echo weird behavior [duplicate]

This question already has answers here:
Variables overwriting text problem with "echo" in Bash
(2 answers)
Closed 4 years ago.
I wrote a script to change .CSV to json.
#!/bin/bash
exec 0< example.csv
while IFS=, read name element input decrease
do
echo "${element}decrease: ${decrease}test"
done
the example.csv I paste here
name1,A,11,12
name2,B,13,14
But the output is really weird...
testrease: 12
testrease: 14
As u can see, The test rewrite Adecrease and Bdecrease, makes them to testrease.
I can't believe it!! So I tried with out exec 0< example.csv, type them in stdin, this time I got what I want
name1,A,11,12
Adecrease: 12test
So I guess maybe there are some characters in example.csv I can't see which makes this problem. I use cat -v example.csv
name1,A,11,12^M
name2,B,13,14^M
Nothing strange and I stuck here.
I am very new to shell script, so if anyone can give me some suggestions I will be really thrilled!!
Thank u, #chepner! tag wiki saves me another hour on this stupid question.
And here is the solution from wiki:
Check whether your script or data has DOS style end-of-line characters.
Use cat -v yourfile or echo "$yourvariable" | cat -v.
DOS carriage returns will show up as ^M after each line.
If you find them, delete them using dos2unix (a.k.a. fromdos) or tr -d '\r'.

Pipelining of cat and ls commands [duplicate]

This question already has answers here:
Unix pipe into ls
(3 answers)
Closed 6 years ago.
I am a newbie to linux . I have been learning about cat command when i tried this .
harish#harish-Lenovo-G50-45:~$ cat file
Videos/Arrow/Season*
harish#harish-Lenovo-G50-45:~$ cat file | ls -l
The command displays the content of the current folder instead of the folder mentioned in the file .. But when i did this
harish#harish-Lenovo-G50-45:~$ cat file
Videos/Arrow/Season*
harish#harish-Lenovo-G50-45:~$ ls -l $(cat file)
The contents of the expected folder displays correctly . Why cant i not use the pipeline in this case ?
In the first example you are passing the output of cat file to the input of ls -l. Since ls -l does not take any input, it does not do anything regarding the output of cat file. However in the second example you are using $(cat file) which puts the output of cat file in the place of an argument passed to ls -l, and this time ls -l has the text inside file in the right place for doing something with it. The issue here is noticing the difference between the standard input of a program and the arguments of a program. Standard input is what you have when you call scanf in C, for example; and the arguments are what you get in the argv pointer passed as parameter to the main procedure.

With a bash script that utilizes system commands; how would you have it input when the COMMAND asks you for something? [duplicate]

This question already has answers here:
Passing arguments to an interactive program non-interactively
(5 answers)
Closed 7 years ago.
So suppose a normal command run in terminal goes like this....
user$ thecommand
Please enter your first name:
>
and then waits for your to type your name... straightforward, but if in a bash script I try and do something like:
#! /bin/bash
echo "What is your name?"
read name
thecommand
how would I have THE SCRIPT enter "$name" in response to "thecommand" instead of having the user manually input it themselves?
you can add input by pipe like this:
echo yourname | ./yourscript
for more inputs you can use printf
printf "input1\ninput2" | ./yourscript
where \n means new line and it will be used like new input.
Run your script like:
./yourscript.sh < file.txt
where file.txt will contain the name.
now your script will look for name from the file(file.txt), in file.txt you can type the names which will act as input for read command.
read command reads on line at a time so if u have more than on read command in your script you should have multiple lines in file.txt file
For complicated cases, for example if your input depends on the output of your command, you may write an "expect" script.
To see how it works you can auto-generate such script interactively
$ autoexpect thecommand
And then run it
$ expect -f script.exp

create a file called hello.txt that contains the word "hello world"?

question is as bellow:-
create a file called hello.txt that contains the words "hello world",. can you use "cp" using "terminal" as the source file to achieve the same effect?
i already create a text file and name as hello.txt. then i type a command in linux terminal asbellow:-
cp hello.txt /home/tobenrry
but it is error and cannot get the output.
i try many command already for creating this. but it is also not work. may i knows what the the command for the question?
On Linux the current terminal is identified by the device /dev/tty, so you can do:
cp /dev/tty hello.txt
That's what the question is asking for.
copy from stdin to file:
cp /dev/stdin $HOME/test.txt
cat > $HOME/test.txt
cat /dev/tty > test.txt
say terminal = /dev/tty
type the following commands:
cat >terminal
"enter the txt here say "hello world" "
ctrl+D
cp terminal destinationfilename

Resources