It seems like such a simple questions but I cant find any resource regarding it
In C/C++ , it means to store the read in a value from command prompt and store it inside a variable ,
How about in Shell Programming?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
The right angle bracket symbol (>) is used to redirect output to a disk file. If the file specified does not already exist, it is created; if it does exist, it is overwritten. The left angle bracket symbol (<) is used to redirect input from a disk file. To append output to an existing file, use double right angle brackets (>>)
>> are redirection operators for FD's (File Descriptors)
In bash you have tree standard FD's that are the standard input (strin), the standard output (strout) and the standard error (strerr). These can also be called by FD 0, FD 1 and FD 2 respectively.
Normally you would have you would have all FD's pointing to the terminal, but this can be changed by using redirection.
For example, if you call:
command > log.txt
You will redirect the output to the file log.txt This is similar as calling:
command 1> log.txt
As this only redirects strout you will still be able to see the error in the terminal. In order to redirect strerr to you log.txt file you will have to run:
command 2> log.txt
Again, this only redirects strerr. If you wish to redirect both stdout and stderr you need to duplicate your stderr output to stdout by using the
command 1> log.txt 2>&1
To understand this command you need to read it form right to left, first a copy of stderr is made to stdout, then strout is redirected to the log.txt file.
When you use redirection in this way, bash will not look if the file exists or not and simply create one regardless if that implies erasing the existing one. If you want to avoid loosing what has already been written in your log file you can then use the
>> command in the same ways explained above, but in this case all the outputs will be appended to existing files.
For their use in C++ with cin, cout and cerr I think hash gave a better answer than I could.
I'm not an expert in these so I might have gotten some things wrong. For a more complete information I advise reading Bash guide on Greg's Wiki
In C/C++ the Left and Right Shift operators use the symbols
>> as the bitwise operator; which perform shift operations on bits. C++ also makes the use of overloaded bitwise shift operators in basic Input/Output operations;
<< brackets in C++ are used for extraction and insertion of data/information to streams which may be standard input/output, files.
In Shell Scripting/Programming, not very different from the extraction/insertion operations as mentioned above, the
<< (variants of
< operators) are used to redirect standard streams from / to those defined by users and perform appending (differing from
< which overwrites) operations.
In shell script the
> operator will create a file that you will put just right to it and will erase every content of the file if exist
>> will append the text to the file that will be right to it
>> will use in writing log file. like You want to add time,count or some process logs
> to create new file try this out:
#!/bin/bash nowt=$(date +"%T") date1=$(date +"%s") NOW=$(date +"%d-%m-%Y") now=$NOW" "$nowt file_name=$NOW"_"$nowt".log" echo "START TIME WAS :$now " | cat > /home/user/Desktop/$file_name sh some_sh_file you want to run 2>&1 | tee >> /home/user/Desktop/$file_name #want to log the out put nowt=$(date +"%T") date2=$(date +"%s") NOW=$(date +"%d-%m-%Y") now=$NOW" "$NOW diff=$(($date2-$date1)) echo "$(($diff / 60)) minutes and $(($diff % 60)) seconds elapsed." echo "END TIME WAS :$now" | cat >> /home/user/Desktop/$file_name echo "$(($diff / 60)) minutes and $(($diff % 60)) seconds elapsed." | cat >> /home/user/Desktop/$file_name