![]() |
bash script troubleshooting
I'm trying to get the following bash script to run, but keep getting this error:
awk: illegal field $(), name "n" input record number 1, file ~/Desktop/myConverter/drop/inputFile.txt source line number 1 Any suggestions on how to get this to work, or for making this cleaner, are welcome. Thanks! Code:
#!/bin/bash |
I guess the problem is with the lines that have:
'NR==$n' since shell variables are not interpolated (substituted for) inside single quotes. And it seems to me that this script will run fairly slowly since it seems to invoke several processes per line. I hope there aren't too many lines in the source file. I would suggest learning Perl and using that - it would be much easier (Perl is designed for text handling) and faster executing. |
Thank you for the feedback.
It seems odd that bash wouldn't be able to handle such a simple task. And the source file is about 30 lines. Are you saying perl is just a little easier, or am I really making it a pain by using bash? Would python be better than perl if I'm going to try to learn a new language? |
Quote:
I personally prefer Perl, but that's mostly because I know Perl. I learned Perl due to the fact that I took over someone else's script at work -- otherwise I would have tossed a coin between the two. Perl's origins lie with sed and awk and other tools, so that might be closer to what you're doing in bash. I don't use bash scripting (or sed or awk) at all, so I'm not really sure what this script is doing. But with a bit more description of your goals I'd be happy to help write a Perl script for this. I'm sure someone else could help with a Python script if that's the way you want to work it. |
i've (loosely) picked up both perl and python. i currently use python, but each has it's own merits and downfalls. i find it useful to know both! :)
if you give a description of what your script is supposed to do, and an example input file, i (or someone else) could show you can example of how to accomplish it in perl/python. |
Thanks all - I'm a perl convert. Here's the beginning of the new perl script:
Code:
#!/usr/bin/perlI'm getting this error: Can't modify constant item in scalar assignment at ./myScript.pl line 3, near "'~/Desktop/myConverter';" Any ideas here? |
Here's a first cut at a Perl script for you - completely untested.
Code:
#!/usr/bin/perl -w1) I added the "-w" to the first line. This makes Perl tell you when you are making silly mistakes. 2) Perl variables need to have a "$" (or other symbol) in front of their names - even when assigning to them. 3) You usually want a "\n" at the end of a print statement. 4) Best to open the sourcefile at the beginning and then process each line. 5) You really need to get a book on Perl. It will be too frustrating to try to learn by trial and error. The usual recommendation is either "Learning Perl" (the Llama book) or "Programming Perl" (the Camel book) - both from O'Reilly. |
i started a python one for you, but i won't post it unless you really want to see it :)
|
Wow - thanks a lot for writing that.
Just wondering what does the 'my' do before each variable assignment? |
Quote:
|
Just FYI, the script you posted spits back the following error:
Use of uninitialized value in open at ./myScript.pl line 7. Use of uninitialized value in concatenation (.) or string at ./myScript.pl line 7. Can't open : No such file or directory at ./myScript.pl line 7. Now to see if I can figure out why... |
not sure if this is the problem, but maybe change this
Code:
my $scriptPath = "~/Desktop/myConverter"Code:
my $scriptPath = "/Users/USERNAME/Desktop/myConverter" |
vancenase - tried switching to the absolute path as you suggested, still no love. Would like to see your Python script, if you feel like sharing...
|
Quote:
Code:
open(SRC, $sourcefile) or die "Can't open $sourcefile: $!";The variable that was initialized was "$sourceFile", but line 7 is using "$sourcefile" (lower-case f). If you now look again at the error messages, you will see that they are telling you what is wrong as well. |
If you're using "my" declarations you might as well go all the way and use strict
Code:
#!/usr/bin/perl -wWhich is what hayne just said. The use of strict means you have to put "my" in front of your locally defined variables -- so it will catch errors such as typos in variable names. It does a whole lot more, but that's the bit relevant to this. |
Cool - thanks a lot guys. Fixed the typo, and tried the path change idea (above), but still get:
Can't open file No such file or directory at ./myScript.pl line 8. Have double-check and triple checked the paths. Is it bad to combine two perl variables and embed them as I'm trying to do, in order to create the path? Does perl not like the '~' character, or does it need to be escaped? |
Quote:
Code:
$some = "/Users";You don't need to escape the ~ character, but Perl might not be able to determine the path based on that. Edit: see below If the path is correct and the file exists just fine, you may want to check permissions. Given the fact that you're using PrivoxyWindowOpen, I'm guessing that you may be using this as a CGI script of some sort. Yes? If so, you'll want to look into permissions and make sure your webserver can read the file. By default Apache runs as user www in group www on OS X. So if your directories have read and execute bits set for either group www or 'other' then you should be OK. If that part doesn't make sense, take a quick peek at the Unix FAQ. Edit to add: Perl doesn't expand the ~ into a real directory, apparently. I tried the following commands: Code:
Aliens:/ nkuvu$ touch ~/foo.txt |
Quote:
|
Quote:
You should follow nkuvu's suggestion and put use strict; as the second line of the script. |
Quote:
I've been seeing this substitution on a number of posts at Perlmonks, and I was wondering where it came from. But I hadn't actually gone in and tried to find out what was going on. Bonus cookies for hayne. Thanks. |
Perl - a great IDE
Just in case:
I found a great IDE that makes developing Perl scripts a breeze. Affrus available at: http://www.latenightsw.com/affrus/index.html The price is high. But I've easily saved development time. |
It can be done in awk
If others don't have immediate time to learn Perl or Python, you could do this:
`awk -v varN=$n 'NR==varN' $sourceFile` |
| All times are GMT -5. The time now is 06:21 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.