LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2017, 12:47 AM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 308
Blog Entries: 2

Rep: Reputation: 16
Question ">" vs "cat /dev/null >"


BACKGROUND
I am reading Advanced Bash Scripting by Mendell Cooper. In this eBook there is a Chapter 16 External Filters, Programs and Commands. Within this chapter there is "Example 16-15. Using tail to monitor the system log".

INTRO
To create the file if it does not already exist, truncate it to zero length (if it exists) and to paste last ten lines of the file /var/log/messages to the aforesaid log file, the author chooses the below method:

TEXTBOOK
Code:
	#NAME LOG FILE
		filename=sys.log

	#TRUNCATES LOG TO ZERO LENGTH
		cat /dev/null > $filename; echo "Creating / cleaning out file."

	#PASTE LAST TEN MESSAGES INTO LOG
		tail /var/log/messages > $filename

But I use the following code line serving the same purpose:

ALTERNATE METHOD
Code:
	#NAME LOG FILE
		filename=sys.log

	#PASTE LAST TEN MESSAGES INTO LOG (Using the ">" to write over the old input with new input).
		tail /var/log/messages > $filename

ANALYSIS
Originally I thought: Isn't truncating the log to zero length by the command: '
Code:
	cat /dev/null > $filename
superfluous being that on the last command a ">" is used and not a ">>"?

But then I remembered "Chapter 3. Special Characters", and on p17 under the Variable expansion/substring replacement section there is the following codeblock:
Code:
: > data.xxx
# File "data.xxx" now empty.
# Same effect as
cat /dev/null >data.xxx
# However, this does not fork a new process, since ":" is a builtin.
So it seems the author is merely trying to broaden our command line flexibility by not repeating the same commands too much.


QUESTION:
By the "fork a new process" sentence above, does this mean that the difference between these two commands is that
Code:
cat/dev/null >data.xxx
does fork a new process BUT
Code:
: > data.xxx
does NOT?

Last edited by andrew.comly; 05-16-2017 at 02:56 AM. Reason: ”NOT" originally left out.
 
Old 05-14-2017, 01:11 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 5,315
Blog Entries: 15

Rep: Reputation: 3107Reputation: 3107Reputation: 3107Reputation: 3107Reputation: 3107Reputation: 3107Reputation: 3107Reputation: 3107Reputation: 3107Reputation: 3107Reputation: 3107
Yes, cat is a command, an actual program found on the command path. By default, any command invoked in the shell creates a new process. To prevent this, you must use the exec builtin. Shell builtins are just input that the shell interprets.

You can distinguish between commands and builtins by using the type command:
Code:
hazel [ ~ ]$ type cat
cat is hashed (/bin/cat)
hazel [ ~ ]$ type :
: is a shell builtin
 
2 members found this post helpful.
Old 05-14-2017, 01:12 AM   #3
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Running

Code:
cat /dev/null > somefile
truncates that file to zero length.

Code:
cat /dev/null >> somefile
doesn't seem to do anything. Theoretically it would append a null to the end of the file, but the file length doesn't increase on my system.


I'm not really familiar with using ":"

Last edited by Laserbeak; 05-14-2017 at 01:13 AM.
 
Old 05-14-2017, 01:14 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,675

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
cat is an external command. An external command involves a fork and the dynamic linker, this is more overhead than a builtin command.
BTW to create/truncate a file you can do
Code:
> data.xxx
No command needed.
And yes,no need for an initial truncation if subsequent commands overwrite the file anyway e.g. with >.
 
3 members found this post helpful.
Old 05-14-2017, 01:49 AM   #5
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Also, I wouldn't assume that "tail" always would give you 10 lines. If you want ten lines, you should say:

Code:
tail -10 somefile
or if you wanted 1000 lines

Code:
tail -1000 somefile
Because I don't know if that's a hard and fast rule that "tail" with no options gives 10 lines. In any case, it's better to make it explicit.
 
2 members found this post helpful.
Old 05-16-2017, 03:34 AM   #6
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 308

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Smile [solved] Internal commands DON'T fork new processes

hazel and MadeInGermany,

Even though I learned the type command, I just never connected it to this real life application.
Code:
$ type cat
cat is /bin/cat

$ type :
: is a shell builtin
Thanks, your reasoning really solved my question.

Last edited by andrew.comly; 05-16-2017 at 03:38 AM. Reason: clarity
 
  


Reply

Tags
bash, fork, truncate


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
script using "/usr/bin/cat error" produces "cannot open" in cron Dcrusoe Programming 6 07-22-2009 03:30 PM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 07:27 AM
Couldn't display "SMB:///" , No host "(Null)" could been Found?????? munkey Linux - Newbie 3 05-03-2004 04:30 PM
RH "null" beta system freezes right after printing "INIT" markus1982 Linux - Hardware 0 09-09-2002 03:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration