Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Is good bash scripting knowledge considered as a "must have" for system admin?

  1. #11
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Is good bash scripting knowledge considered as a "must have" for system admin?

    OK, after doing some more reading, it looks like I could simplify what I wrote greatly as 'read' has a '-a' option to split lines into array elements based on the IFS variable. IFS always seems to trip me up, but this seems to work much better. Also, now that I'm at my work computer, here is the original way (with the improved array building method) that I tried to use that statement, trying to match the number of times a gene was seen in a huge file with the number of counts I have in a table from another source (table reads like "Gene Counts"):

    Code:
    while read -a look; do printf "${look[0]}\t${look[1]}\t"; grep -c -E "^${look[0]}," variants_ENST_170_genelist.csv; done < counts_noENST.txt
    Here's what I think the oneliner I posted here should have been:

    Code:
    while read -a look; do grep -q "${look[0]}" 170_genelist.062413.txt && continue || echo "Not Found: ${look[0]}" ; done < counts_noENST.txt
    Better?

    Anyway, to the OP, learn bash...it's pretty easy, fun, and very, very useful!

  2. #12
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Is good bash scripting knowledge considered as a "must have" for system admin?

    drmrgd, you'd probably be surprised how badly hacked together some of my bash scripts are.

    Rewriting a working script can be a bit... time consuming.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  3. #13
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,425
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Is good bash scripting knowledge considered as a "must have" for system admin?

    Quote Originally Posted by drmrgd View Post
    OK, after doing some more reading, it looks like I could simplify what I wrote greatly as 'read' has a '-a' option to split lines into array elements based on the IFS variable. IFS always seems to trip me up, but this seems to work much better.
    Better?
    yes, read -a was my next idea but seeing that only [0] was needed i opted for read var throwaway_var
    IFS does splitting at [ \t]+ just fine so reading columns is for free (though it would be a pain to read tab delimited data set if null values were to be preserved)

    With regard to the cat call, I didn't know how to load a file into read. Cat didn't seem right there, but I didn't know a better way. That's for pointing out how it should be done!
    Code:
    while read; do ...; done < file
    while read; do ...; done < <( command )   # space between < <
    while read; do ...; done <<< "string"
    <( command ) is a file for all intents and purposes, so any time you see 'file name' in --help of some command, you can use this.
    eg grep -f can take a list of words from a given file
    Code:
    grep -v -f <( awk '{ print $1 }' file1.txt ) file2.txt
    this should find lines of file2 that are not matched by any word from the first column of file1
    Last edited by Vaphell; June 26th, 2013 at 07:33 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #14
    Join Date
    Jun 2013
    Beans
    19

    Re: Is good bash scripting knowledge considered as a "must have" for system admin?

    Whoa,

    Thank you all very much for your replies.

    I am super convinced that bash indeed will help.

    On to it now.

    Thank you very much everyone. This is why Ubuntu rocks. It has amazing community and forum members.

    Awesome.

  5. #15
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Is good bash scripting knowledge considered as a "must have" for system admin?

    Quote Originally Posted by CharlesA View Post
    drmrgd, you'd probably be surprised how badly hacked together some of my bash scripts are.

    Rewriting a working script can be a bit... time consuming.
    No doubt! And being OCD doesn't make matters any better. I guess in the end if you got the answer you needed, even if the script is a bit ugly or not as eloquent as it could be, it's good enough.

    Code:
    
    while read; do ...; done < file
    while read; do ...; done < <( command )   # space between < <
    while read; do ...; done <<< "string"

    <( command ) is a file for all intents and purposes, so any time you see 'file name' in --help of some command, you can use this.
    eg grep -f can take a list of words from a given file
    Thanks again for clarification and education. You've really, really simplified this for me very nicely and helped to make some better scripts!
    Last edited by drmrgd; June 26th, 2013 at 08:34 PM.

Page 2 of 2 FirstFirst 12

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
  •