Re: perl callable from bash?
by ikegami (Patriarch) on Apr 13, 2009 at 05:11 UTC
|
echo "[$firname]"
| [reply] [d/l] |
Re: perl callable from bash?
by ig (Vicar) on Apr 13, 2009 at 01:20 UTC
|
Maybe your scripts (firstscript and secondscript) have <CR><LF> line terminations instead of the usual (on unix) <LF> line termination. That might explain the first of your error messages. The second error message, beginning without an opening single quote and with the text ".links" is a bit more puzzling.
One way to see what your line terminations are is to use the "od" command. For example, in the following, each line ends with "\r\n".
$ od -c test.html
0000000 < h t m l > \r \n < h e a d > \r \
+n
0000020 < t i t l e > t e s t < / t i
+t
0000040 l e > \r \n < / h e a d > \r \n <
+b
0000060 o d y > \r \n T h i s i s s
+o
0000100 m e " t e x t " i n t h
+e
0000120 b o d y . \r \n < / b o d y > \
+r
0000140 \n < / h t m l > \r \n
0000152
After converting the line endings, it appears as follows:
$ od -c test.html
0000000 < h t m l > \n < h e a d > \n <
+t
0000020 i t l e > t e s t < / t i t l
+e
0000040 > \n < / h e a d > \n < b o d y
+>
0000060 \n T h i s i s s o m e "
+t
0000100 e x t " i n t h e b o d
+y
0000120 . \n < / b o d y > \n < / h t m
+l
0000140 > \n
0000142
Now the lines end with "\n" only.
Depending on your unix installation, you may have a command "dos2unix". I used the vim editor. You might also be able to use the "tr" command as "tr -d '\r' <file.old >file.new"
How did you create your perl script files? | [reply] [d/l] [select] |
|
Normally I'd use Emacs, but since the target is down I'm using EditPlus2 which can switch hit. Also using Cygwin to emulate the real environment. Don't think I mentioned it, but both scripts run fine from the command line. Just not in the shell script.
--hsm
"Never try to teach a pig to sing...it wastes your time and it annoys the pig."
| [reply] |
Re: perl callable from bash?
by eye (Chaplain) on Apr 13, 2009 at 06:15 UTC
|
Two points to be made here.
First, you rarely, if ever, want to set a script to have permissions (mode) 777. That give write access to your script to anyone with access to the machine. A mode of 755 should be sufficient to allow anyone to run the script. If it is just for you, you could use 700.
Second, I'm going to take a wild stab at your actual question and suggest that you:
- change the script to include the absolute paths of the scripts (e.g., /home/hsmyers/bin/firstscript
- enclose the name of your file in single quotes on the command line
Good luck!
| [reply] |
Re: perl callable from bash?
by FunkyMonk (Chancellor) on Apr 13, 2009 at 09:12 UTC
|
If you've copied your file to /bin you should be able to run it by just typing bl filename. Using ./bl tells the shell to run the file called bl that's in the curent directory.
The way I do it is to make my own bin in my home directory with mkdir $HOME/bin and add it to my path by adding export PATH=$HOME/bin:$PATH at the end of $HOME/.bashrc assuming you're using bash as your shell. You still need to make the executable using chmod as previously mentioned. | [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
While .profile and $HOME/bin are quite appropriate for an individual developer, a sysadmin faces a different situation. If the script is intended to for use by all users of the system, the traditional location is /usr/local/bin or maybe even /usr/local/sbin. If the file is required for boot, then /bin or /sbin might be called for. The caveat with .profile is that it's only invoked by an interactive shell.
When I have a script that I only want root to run, I'll usually toss the script in /root/bin unless the system uses / as root's home directory.
I'm not really disagreeing, except with the idea that there is a single best practice for all situations.
| [reply] |
|
Re: perl callable from bash?
by irah (Pilgrim) on Apr 13, 2009 at 04:00 UTC
|
how to make both firstscript and secondscript executable (chmod 777 and mv to /bin)
If you are using Linux, you can create a symbolic link for that file, and put it in /bin directory.
For execute a Perl script, give the exact path of Perl script in the bash script and check it.
| [reply] |
Re: perl callable from bash?
by Bloodnok (Vicar) on Apr 13, 2009 at 10:21 UTC
|
Apart from the advice already proffered elsewhere on this thread, I'm intrigued as to how you can ...mv to /bin and then expect the script to run by entering ./bl filename without first cd(1)'ing to /bin aka having just moved the script from your current working directory, you expect to still be able to invoke the script in there &91;your current working directory&93;
Moreover, the 2 scripts, called by the shell script, are no longer in the same directory as the shell script - hence it should come as no surprise that the shell script can no longer find them, so assuming all 3 scripts were previously in your home directory, I would modify the shell script (now /bin/bl) to read...
#!/bin/sh
filename=$HOME/$1
firstscript $filename
secondscript $filename
A user level that continues to overstate my experience :-))
| [reply] [d/l] [select] |
Re: perl callable from bash?
by targetsmart (Curate) on Apr 13, 2009 at 13:45 UTC
|
Which I at first thought had to do with not getting the variable right--- but a little detective work convinced me that what it couldn't find were the two scripts.
IMO you are wrong, if you don't have script in place, the shell script will say
./check.sh: line 3: firstscript: command not found
./check.sh: line 4: secondscript: command not found
IMO you have problem with arguments only, and might be some problems with the absolute path of the first and second script(as other monks mentioned).
generally if you are dealing with filenames in shell scripts, just check it existence before using it for further processing.
you can use something like
-e file
True if file exists.
-f file
True if file exists and is a regular file.
-r file
True if file exists and is readable.
-s file
True if file exists and has a size greater than zero.
-O file
True if file exists and is owned by the effective user i
+d.
-G file
True if file exists and is owned by the effective group
+id.
and more of this can be seen in your shell manual(the above operators are from bash manual), use those effectively to figure out whether the file exists and continue only in such case. For the script use
-x file
True if file exists and is executable.
before calling it from your shell script.
if I write your script (shown in OP), in bash, it will be like
#!/bin/bash
echo -e "received arguments >$*<"
filename=$1;
script1='fristscript'; # use absolute path
script2='firstscript'; # use absolute path
if [ -f $filename ] ; then
echo -e "executing scripts $script1, $script2";
if [ -x $script1 ]; then
echo -e "executing $script1..."
$script1 $filename
else
echo -e "$script1 not found";
fi
if [ -x $script2 ]; then
echo -e "executing $script2..."
$script2 $filename
else
echo -e "$script2 not found";
fi
else
echo -e "$filename is not readable or doesn't exist";
fi
Hope this helps.
(again the above bash code can be written even more effectively, but that is not the point of discussion)
Vivek
-- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
| [reply] [d/l] [select] |