Find the answer to your Linux question:
Results 1 to 8 of 8
Hi all; I am having all sorts of trouble trying to assign a variable within an awk script with the system command. I know there is a lot of ways ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    mrz
    mrz is offline
    Just Joined!
    Join Date
    Nov 2010
    Posts
    7

    assigning bash variable with the system command in awk


    Hi all;

    I am having all sorts of trouble trying to assign a variable within an awk script with the system command. I know there is a lot of ways around this problem, but for efficiency reasons, I would like to, within my awk script, do something like

    system(x=3)

    or

    system(x=NR)


    and, latter on the shell script which calls the awk script, use the variable $x. But nothing is passed to x.

    I have already tried things like

    command = "x=3"
    system(command)

    and also used a pipeline within the system to pipe it to /bin/sh

    In fact tried a lot of stuff like that, using $(( )) etc etc etc

    Funny thing is that I can create directories e write to files (yes, i could write to a file and read from there, but I dont think it is efficient, plus I am puzzled).

    I guess it has something to do with a shell sub-process.

    Thanks a lot in advance.

  2. #2
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    just guessing, but system(export X=3) may saves the day?

  3. #3
    mrz
    mrz is offline
    Just Joined!
    Join Date
    Nov 2010
    Posts
    7
    Good guess, but it also do not work. I guess the point here is export a variable from a shell sub-process to its parent process (maybe if this was the title of the thread a lot of people would be giving their shots).

    From the parent to the child you do it with export, the other way around seems to be tricky.

  4. #4
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    thinking about it: the other way around should be impossible as that would be dangerous and would create big problems as it would be the easiest way to manipulate a system.

    btw: shouldn't the awk script know what to do and output proper data that (if wanted) could be parsed by the invoker?

  5. #5
    mrz
    mrz is offline
    Just Joined!
    Join Date
    Nov 2010
    Posts
    7
    I see your point, but I guess it is not impossible. But surely I would have to make my parent process read from the child (and this is one thing I was missing at first).

    In general it seems that a coprocess would be the way around this, but you cannot use it with awk. Anyway I found another way to solve my problem, but after your guess on the 10:27 post I learnt a lot of things. Thanks!

  6. #6
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    glad to have helped .. don't know how but glad anyway.

    solve the thread once you believe it is appropriate to do so.

  7. #7
    mrz
    mrz is offline
    Just Joined!
    Join Date
    Nov 2010
    Posts
    7
    It was after your post that I realised the obvious (that awk aws run in a child process), that's how you've helped (and thanks also for the suggestions themselves).

    As for the "solved", although I found another way around my particular problem, the original question is still open, and I think it is worth answering.

  8. #8
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    I do not believe there exists a solution for above usecase, but maybe we've got a brainy guy out there who knows.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •