How can I convert my shell script to Perl? [closed] - perl

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a shell script:
#!usr/bin/bash
branch_name=$1
task_name=$2
createviewset ccm -b $branch_name -t $task_name
source setenv $task_name
rest of the code
Now I want to convert this script to a Perl script. How can I do that? So far what I have written in the code but this code does not seem to work.
!/usr/bin/perl
use warnings;
use strict;
my branch_name;
my task_name;
createviewset ccm -b $branch_name -t $task_name
source setenv $task_name
Here the createviewset is an existing script which I am calling here.

You should visit http://perlmaven.com/ (available in many languages) or http://learn.perl.org/ to learn some Perl first.
Your shell script doesn't need to copy the commandline values. You also used #!usr/bin/bash which won't work because the path is either /usr/bin/bash or (more common) /bin/bash:
#!/bin/bash
createviewset ccm -b $1 -t $2
source setenv $2
rest of the code
Perl assigns all command line arguments to the array #ARGV. This sample prints your two arguments:
#!/usr/bin/perl
print $ARGV[0];
print $ARGV[1];
Notice that the numbering starts with 0 instead of 1 as in $1 in your bash script.
Next part is running external (shell) commands in Perl: Use the system command.
#!/usr/bin/perl
use strict;
use warnings;
system 'createviewset','ccm','-b',$ARGV[0],'-t',$ARGV[1];
system 'source','setenv',$ARGV[1];
Notice that the source command won't work because a Perl script is not a shell script and can't "include" Bash script. I appreciate that you're trying to use Perl for your problem, but it looks like Bash is the much better tool for this.

Related

Modify print job with one liner - missing output command [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am using Perl with our printing output system. Usually we call perl scripts, but I am just replacing a keyword with another word. In a console and a test file this worked with this one liner:
perl -pi -e "s/KEYWORD1/KEYWORD2"
Now I wanted to use it with our system and it gives me the error that there is no output file created. As I am fairly new to the field of Perl and want to have it used within a one liner, how can i do this?
Thank you very much for your help.
Kind regards
As Shawn mentioned in the comment, you are missing / in the substitution command s///. You are also missing a filename, which is needed for the -i option.
Both of these fail and print a warning and an error:
perl -pi -e "s/KEYWORD1/KEYWORD2" < foo
# or:
cat foo | perl -pi -e "s/KEYWORD1/KEYWORD2"
They print messages:
-i used with no filenames on the command line, reading from STDIN.
Substitution replacement not terminated at -e line 1.
Correct usage:
perl -pi -e 's/KEYWORD1/KEYWORD2/' foo
# or:
perl -pe 's/KEYWORD1/KEYWORD2/' < foo
# or:
another_command | perl -pe 's/KEYWORD1/KEYWORD2/'

sed -i on FreeBSD [duplicate]

This question already has answers here:
sed in-place flag that works both on Mac (BSD) and Linux
(15 answers)
Closed 4 years ago.
I have got strings stored in two variables (line is current line and new is the replacement). My code looks like this :
sed -i "s#line#new#" output_file
However, this solution does not work on FreeBSD.
Is there any way to modify this code just a bit so it would work?
Yes. Use:
sed -i '' "s#line#new#" output_file
On BSD systems (and macOS too), the sed command's -i option requires a suffix, which may be attached to the -i or a separate argument. However, when the suffix is empty, it must be a separate argument. This is different from GNU sed, where the -i option takes an optional suffix, but if specified, it must be attached to the -i option. Scripts portable between the two (BSD and GNU) therefore must be written with an explicit non-empty suffix attached to the -i option. Note that such scripts may still be unportable to other POSIX systems; the -i option is not standardized (as you can tell from the divergent behaviour).

A change of shebang + eval leads to the perl script failure

This is a question derives from another post. Based upon the comments and answer from that post, I change the following shebang + eval into another one:
Old works version
#!/bin/perl
eval 'exec perl5 -S $0 ${1+"$#"}'
if 0;
New doesn't work version
#!/bin/sh
eval 'exec perl5 -S $0 ${1+"$#"}'
if 0;
Notice that I change #!/bin/perl to #!/bin/sh and based upon my understanding, the new version should also work because the script is treated like shell script and eval get executed and perl5 is invoked to use perl to execute the same script. However, when I actually run this, I got:
/bin/sh: -S: invalid option
Can anyone explain why this case the script is failed. Do I misunderstand something? I'm using ksh
Also this web page I found online seems suggest that my new version should work as well.
Thanks much!
From the Perl documentation:
If the #! line does not contain the word "perl" nor the word "indir", the program named after the #! is executed instead of the Perl interpreter. This is slightly bizarre, but it helps people on machines that don't do #!, because they can tell a program that their SHELL is /usr/bin/perl, and Perl will then dispatch the program to the correct interpreter for them.
So if you launch the modified version as ./script:
Your shell executes ./script
The kernel actually executes /bin/sh ./script
sh executes perl5 -S ./script
perl5 executes /bin/sh -S ./script because it sees a shebang that doesn't contain perl.
sh dies because it doesn't recognize the -S option.
And if you launch the modified version as perl5 script:
Your shell executes perl5 script
perl5 executes /bin/sh -S script because it sees a shebang that doesn't contain perl.
sh dies because it doesn't recognize the -S option.
Also this web page I found online seems suggest that my new version should work as well.
The code on that page is significantly different than the code you used. In that code, there's an explicit instruction (-x) to ignore the actual shebang line, and to look for one that contains perl later in the file (which is also missing from your code).

write in file and STDOUT from perl script [duplicate]

This question already has answers here:
How to get perl code output to STDOUT/STDERR and a file, in realtime and cross-platform?
(5 answers)
Closed 9 years ago.
Hi I need to have perl script's output in both file and STDOUT but I cannot redirect output from shel like ./a.pl > out.log. Is it possible?
Obviously, you should be using
./a.pl | tee out.log
but it sounds like you will reject that. Next best is probably File::Tee.
use File::Tee qw( tee );
tee(STDOUT, '>', 'out.log');
Try this instead:
a.pl | tee out.log

Difference between "perl" and "perl -w"?

I am learning Perl , very new user . May i know whats the difference between these Perl codes.
#!/usr/bin/perl
&
#!/usr/bin/perl -w
That is not perl code, it's a shebang, which is used in a linux/unix environment as a way to tell the shell what program should be used to run the program file. It has no effect in windows, but it does activate any switches used.
The -w part is a switch for perl, telling it to activate warnings. You can learn more about perl's command line switches by typing perl -h at the command prompt. Read more in perldoc perlrun
-w is the older version of the use warnings pragma, which is preferred nowadays. While -w is global, use warnings is lexical, and can be activated selectively.