Results 1 to 7 of 7

Thread: Shell scripting trouble

  1. #1
    Join Date
    Dec 2007
    Location
    ~
    Beans
    314
    Distro
    Ubuntu 10.04 Lucid Lynx

    Exclamation Shell scripting trouble

    I want to pass the output of
    Code:
     grep -P "^[a-zA-Z0-9:]+\s+package" ctags_output.proc | awk '{print $1}'
    to a script run.sh as its second argument ie
    Code:
     ./run.sh pkg <argument>
    what i am doing now is
    Code:
     grep -P "^[a-zA-Z0-9:]+\s+package" ctags_output.proc | awk '{print $1}'| xargs ./run.sh pkg
    but this is not working! what am i doing wrong??

  2. #2
    Join Date
    Jan 2009
    Beans
    Hidden!

    Re: Shell scripting trouble

    If the output from the command is just supposed to provide arguments for run.sh, there's no need to use xargs. You can just use bash command substitution.

    If you want the output to provide multiple white-space separated tokens:
    Code:
    ./run.sh pkg $(grep -P "^[a-zA-Z0-9:]+\s+package" ctags_output.proc | awk '{print $1}')
    If you want one single argument, then quote the command substitution expression:

    Code:
    ./run.sh pkg "$(grep -P "^[a-zA-Z0-9:]+\s+package" ctags_output.proc | awk '{print $1}')"
    If it still doesn't work, then run your script with the -x argument to get debugging output so you can figure out what's going wrong, as in:

    Code:
    bash -x ./run.sh pkg $(grep -P "^[a-zA-Z0-9:]+\s+package" ctags_output.proc | awk '{print $1}')

  3. #3
    Join Date
    Dec 2007
    Location
    ~
    Beans
    314
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Shell scripting trouble

    no.... i want every line of the output to got to a different ./run.sh so i was using the xargs!

  4. #4
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Beans
    71

    Re: Shell scripting trouble

    If every line of output should go to separate instance of run.sh, then you can do it with a bit of shell scripting as follows:

    Code:
    for arg2 in $(grep -P "^[a-zA-Z0-9:]+\s+package" ctags_output.proc | awk '{print $1}')
    do
       ./run.sh pkg $arg2
    done

  5. #5
    Join Date
    Jan 2009
    Beans
    Hidden!

    Re: Shell scripting trouble

    Quote Originally Posted by nebu View Post
    no.... i want every line of the output to got to a different ./run.sh so i was using the xargs!
    Ah ... well, in that case, you should be using the '-L max-lines' option for xargs, or the for loop quoted above.

  6. #6
    Join Date
    Aug 2010
    Location
    Hopewell Junction, NY
    Beans
    332
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Shell scripting trouble

    Another way:

    Code:
    grep -P "^[a-zA-Z0-9:]+\s+package" ctags_output.proc | awk '{print $1} | while read x
    do
         ./run.sh pkg $x
    done

  7. #7
    Join Date
    Aug 2007
    Location
    Sweden
    Beans
    197
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Shell scripting trouble

    Option -l to xargs will run command once for each line in the input.
    Option -n1 to xargs will run command once for each word in the input.
    I think any would work in this case.

Tags for this Thread

Bookmarks

Posting Permissions

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