0

I am new to perl.Below is my code to touch a directory using perl

#!/usr/bin/perl

# Populate the hashMap
# keys          -->  directories to clean
# attributes    -->  days to keep.



print "<-------------------------------------------------------->";
print " Touching of folders Started   ";
touch `/tmp/dir1`;
touch `/tmp/dir2`;
print " Touching of folders Ended   ";
print "<-------------------------------------------------------->";

Get the below syntax error when I run the script. Please help.

$ ./FeedServerHscript.sh
Backticks found where operator expected at /tmp/eCAS_Housekeep/Ecas_54.pl line 11, near "touch `/tmp/dir1`"
        (Do you need to predeclare touch?)
Backticks found where operator expected at /tmp/eCAS_Housekeep/Ecas_54.pl line 12, near "touch `/tmp/dir2`"
        (Do you need to predeclare touch?)
syntax error at /tmp/eCAS_Housekeep/Ecas_54.pl line 11, near "touch `/tmp/dir1`"
Execution of /tmp/eCAS_Housekeep/Ecas_54.pl aborted due to compilation errors.
serenesat
  • 4,611
  • 10
  • 37
  • 53
  • 2
    http://perldoc.perl.org/perlfunc.html#utime-LIST – TLP Nov 05 '15 at 08:14
  • Similar to [How to touch a file and mkdir if needed in one line](http://stackoverflow.com/q/28296411/1824796). And why [touch a directory](http://askubuntu.com/q/366780), just create it `perl -MFile::Spec -e 'mkdir File::Spec->catpath(undef, "/tmp", "dir001") or die q(err: cant do that)'`. – Pavel Nov 05 '15 at 08:58
  • 2
    Use `use strict; use warnings;` in every Perl script. – reinierpost Nov 05 '15 at 10:13

1 Answers1

5

Use system command instead of backtick, if you don't want return value in a variable.

print "<-------------------------------------------------------->";
print " Touching of folders Started   ";
system("touch /tmp/dir1");
system("touch /tmp/dir2");
print " Touching of folders Ended   ";
print "<-------------------------------------------------------->";

Use backtick like this:

my $dir2 = `touch /tmp/dir1`;
my $dir2 = `touch /tmp/dir2`;

See this for more understanding about system and backtick: https://stackoverflow.com/a/800105/4248931

Community
  • 1
  • 1
serenesat
  • 4,611
  • 10
  • 37
  • 53