I just stumbled upon a difference between using awk on the commandline and using it in a shellscript.
I have a variable, e.g.: PROG=vim
then i want to check if the package with this name is installed: TEMPVAL=$(dpkg -l | awk '{ if ($2 == "$PROG") print $2 }') (Im using Tempval later in an condition to check if it is installed)
Running this on the commandline works fine. In the shellscript TEMPVAL is always empty.
btw "toinstall" just contains some package names to be installed and comments prefixed with #
full script:
bash -x output:
Whats the Problem? I already tried playing with the quotation, but nothing worked. Hope somebody can help me. I'd also appreciate hints or tips if theres a more convenient way of doing what i want to do
This article describes three ways to interface AWK programs with shell scripts and how to import shell variables into AWK programs.
But a little better:
The inner double quotes are really not needed here but this is a good habit.
It's actually a pretty bad habit -- there's a much better way to get variables into awk, -v. No need to mess with ending and beginning single and double quotes inside the awk script itself.
I wrote about quoting shell variables not about embedding.
Maybe you true about awk but the "pretty bad" way is universal - it's for perl, sed, python or almost anything other, where you want to embed shell variables.
I wrote about quoting shell variables not about embedding.
Maybe you true about awk but the "pretty bad" way is universal
It's a bad idea because it becomes language syntax, not merely a string. This is especially bad for a variable, where you might not have strict control over the exact contents:
Quote:
it's for perl
Quote:
...sed, python or almost anything other, where you want to embed shell variables.
I admit there's no good way to do it in sed, but this is an argument against sed, not an argument for that syntax. It's very difficult to safely put variables in sed because so many things need escaping.
And if the argument is that you don't need to memorize syntax for each language, well, you already had to memorize -e for perl, how much harder is -v, shift, etc?
Last edited by Corona688; 06-29-2011 at 01:42 PM..
In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13.
I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Hi,
I'm trying to write a script to determine the time gap between HTTP PUT and HTTP DELETE requests in the HTTP Servers access log.
Normally client will do HTTP PUT to push content e.g. file_1.txt and 21 seconds later it will do HTTP DELETE, but sometimes the time varies causing some issues... (3 Replies)
Hello all,
I have a question regarding the difference betwen cron and command line.
What I would like to do is to print a statement into a logfile if a script has been executed from cron or from command line.
It should be as:
#!/bin/bash
if <Check if this script has been... (3 Replies)
solaris 5.10 Generic_138888-03 sun4v sparc SUNW,Sun-Fire-T200
I need a sed command that tests true when presented with lines that contain either forward and backslash.
input file:
c:/myFile.txt
c:\yourFile.txt
It doesn't appear that sed (in my environment anyway) supports... (4 Replies)
Hi..I have the data in a file like in this format, and I need the output time difference in seconds by using awk command. Start date/time and end date/time given in column 2,3 & 4,5. Please assist how to write shell script.
File1.txt
JOB1 10/09/2013 17:42:16 10/09/2013 17:43:46 SU 6202685/1... (4 Replies)
Hi All,
I am working on nawk script, has the small function which prints the output on the screen.Am trying to print/append the same output in a file.
Basically nawk script should print the output on the console/screen and as well it should write/append the same result to a file.
script :... (3 Replies)
Hi there,
I have a wapper script which passes the argument from command prompt to inner script.. It works fine as long as the argument containing single word. But when value contains multiple word with space, not working as expected. I tried my best, couldn't find the reason. Gurus, pls.... (2 Replies)
Hi,
I need to know how I'll be able to write a script that can goto a different dir where I don't have access to read,write and execute and also to run a commandline prompt in that dir with one file whose path has to be specified in that command.
Will I be able to do this?
Any ideas or... (2 Replies)