Solved

touch command in perl script

Posted on 2010-09-03
16
6,657 Views
Last Modified: 2013-12-26
I want to run touch a file command like the following.  The file name is parameter passed into a perl script.  How to write the following?  Thanks.

touch $ARGV[0]  
0
Comment
Question by:ewang1205
  • 4
  • 3
  • 2
  • +6
16 Comments
 
LVL 14

Expert Comment

by:Michael Dyer
ID: 33599726
Try this
                 #!/usr/bin/perl
                 $now = time;
                 utime $now, $now, @ARGV;
0
 

Author Comment

by:ewang1205
ID: 33599832
What does your script do?  Thanks.
0
 
LVL 10

Expert Comment

by:jeromee
ID: 33600065
From the Perl doc:
 utime LIST
             Changes the access and modification times on each
             file of a list of files.  The first two elements of
             the list must be the NUMERICAL access and
             modification times, in that order.  Returns the
             number of files successfully changed.  The inode
             change time of each file is set to the current time.
             For example, this code has the same effect as the
             Unix touch(1) command when the files already exist
             and belong to the user running the program:

                 #!/usr/bin/perl
                 $atime = $mtime = time;
                 utime $atime, $mtime, @ARGV;

             Since perl 5.7.2, if the first two elements of the
             list are "undef", then the utime(2) function in the
             C library will be called with a null second
             argument. On most systems, this will set the file's
             access and modification times to the current time
             (i.e. equivalent to the example above) and will even
             work on other users' files where you have write
             permission:

                 utime undef, undef, @ARGV;

             Under NFS this will use the time of the NFS server,
             not the time of the local machine.  If there is a
             time synchronization problem, the NFS server and
             local machine will have different times.  The Unix
             touch(1) command will in fact normally use this form
             instead of the one shown in the first example.

             Note that only passing one of the first two elements
             as "undef" will be equivalent of passing it as 0 and
             will not have the same effect as described when they
             are both "undef".  This case will also trigger an
             uninitialized warning.
0
 

Author Comment

by:ewang1205
ID: 33600110
I don't see it can solve my original issue though.  Thanks.
0
 
LVL 3

Accepted Solution

by:
gremwell earned 300 total points
ID: 33600145
The problem is that utime() function does not create a file. The attached code should do the job. To get more details about what particular function of class does try 'perldoc' command (i.e. 'perldoc IO::File' or 'perldoc -f utime').

#!/usr/bin/perl -w

use strict;
use IO::File;

my $fname = shift @ARGV;

unless (-f $fname) {
	my $f = new IO::File($fname, "a") || die "open: $!";
	$f->close();
}

my $now = time();
utime($now, $now, $fname) || die "utime: $!";

Open in new window

0
 
LVL 10

Assisted Solution

by:jeromee
jeromee earned 50 total points
ID: 33600266
Sorry, ewang1205.
I didn't realize that you wanted to use touch to create an empty file... which's not touch's primary function.

User Commands                                            touch(1)

NAME
     touch, settime - change file access and modification times

SYNOPSIS
     touch [-acm] [-r ref_file | -t time]  file...

     touch [-acm] [date_time] file...

     settime [-f ref_file] [date_time] file...

0
 

Author Comment

by:ewang1205
ID: 33600339
Thanks.  Yes, I want to use touch to create a file.  I am testing gremwell's solution.  
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 50 total points
ID: 33601514
A simpler version of gremwell's solution is:


die "no filename specified" unless @ARGV;

open my $file, '>', $ARGV[0] or die "can't create '$ARGV[0]' $!";

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:vikaskhoria
ID: 33602088
If you just want to use the touch command as a unix command then you can use system as well.

Try this:

system("touch ".$ARGV[0]);

Read more on this at:
http://www.perlhowto.com/executing_external_commands
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 33602090
Unlike touch,
open my $file, '>', $ARGV[0]
will truncate a file if it exists.

If portability and efficiency is not an issue, you could do
system "touch $ARGV[0]";
0
 
LVL 3

Expert Comment

by:gremwell
ID: 33602293
> system("touch ".$ARGV[0]);
> system "touch $ARGV[0]";

Just for the sake of correctness -- the code above may have difficulties handling some special cases, such as file names containing white spaces and file names starting with dash character. To cover for it, system() should be invoked as following: system("touch", "--", $ARGV[0]);
0
 
LVL 84

Expert Comment

by:ozo
ID: 33602313
If the touch command is considered the standard of correctness, the code above handles those cases the same way that the touch command does.
It might be argued that
system "touch",@ARGV;
would be more correct, but the original specification did not state anything beyond $ARGV[0] should be interpreted
0
 
LVL 5

Assisted Solution

by:fredmc
fredmc earned 50 total points
ID: 33603811
Anything wrong with just back ticks?
`touch -- "$ARGV[0]"`

using -- and quotes to deal with files starting with a dash, and with white space.



0
 
LVL 3

Expert Comment

by:gremwell
ID: 33603994
@ozo: You are right, ewang1205 has asked for equivalent of UNIX touch command, perhaps he wanted to simulate all its quirks as well.

@fredmc: As a matter of fact, the back ticks example will have problems with file names containing double quotes ;).
0
 

Expert Comment

by:access_choudhary
ID: 33607281
Suggest you add utime in addition to open/close, since if the file already exists you don't need to open and close it, you can simply call the utime built-in function to change its access and modification times.

Other way is to use  Shell::Command wrapper around ExtUtils::Command

use Shell::Command;
touch @files;
0
 

Author Closing Comment

by:ewang1205
ID: 33639683
Thanks for all the help.  I have not tested each cases yet.  But, I think most of them will work fine.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now