Fred Stluka on 24 Aug 2018 14:00:15 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] shell scripting help


JP,

Lots of good tips here.  Thanks!

--Fred
------------------------------------------------------------------------
Fred Stluka -- Bristle Software, Inc. -- http://bristle.com
#DontBeATrump -- Make America Honorable Again!
------------------------------------------------------------------------

On 8/24/18 1:50 PM, JP Vossen wrote:
Ouch, sorry to hear about the Windows part.  :-)  I'm forced to use a Windows laptop at $WORK and loath it, I do all my real work on a Mint VM thought running it on top of Windows is like building a house on sand (I snapshot it a lot).

I know you got your script working, but all those greps are inefficient because each `|` is another sub-shell.  The following is untested since I don't have your data, and I merged all the `grep -v` to *before* the sort which might break something though I don't see how it could.  I also broke the long lines before some MUA or MTA does it for me.  Don't use before testing well!  Of course.

Prompted:
----
#!/bin/bash -
# Add date and comment here

read -p "What is the filename you want to grep? " file_name
egrep '\b[A-Z]+| [0-9:]+ \b' "$file_name" \
  | egrep -v 'anaged|No|Na|Ser|Ty|Contact' | sort -u
----

From command line:
----
#!/bin/bash -
# Add date and comment here

# Usage:: <script name> <file name>
egrep '\b[A-Z]+| [0-9:]+ \b' "$1" \
  | egrep -v 'anaged|No|Na|Ser|Ty|Contact' | sort -u
----

Various rambling:

Always add at least a comment and ideally a day, describing your *intent*.  You will thank yourself 6 months from now.

You can use `read -p` to get rid of the `echo`.

Try `help read` since `man read` will probably give you the Bash man page, which is good reading but not really helpful in a case like this. `help` gives help on bash built-ins like `read`, `set`, `test` (really useful) and lots more.

As pointed out elsewhere, giving it the file name on the command line is more Unix-y, so there's a versions does that.  Actually, using it as a "filter" would be even more Unix-y, but one thing at a time.  :-)

There is also obviously zero sanity or error checking, though in this case that's not a big deal.

You don't need ';' statement terminators.  They don't hurt, but they are unnecessary noise.

The trailing "-" in the shebang line `#!/bin/bash -` protects against an ancient kernel vulnerability.  I'm not even 100% sure it still matters, but it can't hurt and it's arguably a good habit.

Semi-related, I like doing "top N" lists like:
    <stuff> | sort | uniq -c | sort -rn | head -n10


There are tools (getclip.exe and putclip.exe) that let you read/write the windows clipboard from the command line.  I've never tried those in WSL, but they used to be GREAT with the UnxUtils to grab a column of, say, IPAs from a spreadsheet and:
    getclip | sort | uniq -c | sort -rn | head -n10 | putclip

See my very old and not really relevant to WSL pages at:
* https://www.jpsdomain.org/windows/win-tools.html
* https://www.jpsdomain.org/windows/winshell.html


On 08/24/2018 12:42 PM, Michael Lazin wrote:
Thank you all, it's working now.  I actually am not in the role of being a Linux user right now. Maybe that is why interactive scripts appeal to me lol.  I did forensics on Debian web servers for 10 years and lost my job and ended up finding a job as a Windows sysadmin rather quickly and took it because it beats no job.  I installed the linux subsystem for Windows because I do love bash and have found it useful for getting some mundane tasks done.  I am also learning powershell, I know it stands against everything in this group, but I keep telling myself knowing two OS's will eventually work to my advantage.  Thanks.

On Fri, Aug 24, 2018 at 12:28 PM, prushik <prushik@gmail.com <mailto:prushik@gmail.com>> wrote:

    Very few Linux users enjoy interactive scripts, but whatever floats
    your boat.
    Your script has a few issues, but the biggest is that you seem to be
    confusing bash and php. $ is not a variable indicator in bash, its
    an expansion operator. This means that your read $i line gets
    expanded to read "", since i is unset at the beginning of the script.

    Also remove done from the end of the script since you have no
    corresponding do.



    Sent from my device.
    ---- Original message ----
    From: Anthony Martin <anthony.j.martin142@gmail.com
    <mailto:anthony.j.martin142@gmail.com>>
    Sent: 08/24/2018 12:12:24
    To: Philadelphia Linux User's Group Discussion List
    <plug@lists.phillylinux.org <mailto:plug@lists.phillylinux.org>>
    Subject: Re: [PLUG] shell scripting help

    Have you tried setting it so you give the file name as an argument
    when you run the script? ./Script FILENAME

    Anthony Martin

    Linux System Administrator



    On Fri, Aug 24, 2018 at 12:04 PM Michael Lazin <microlaser@gmail.com
    <mailto:microlaser@gmail.com>> wrote:

        I am trying to write a shell script to automate the formatting
        of a text file in a certain way that is useful to me. the
        complicated egrep while not ideal does the job when run alone,
        where $i is the name of the file I want to grep, but when I try
        put it together in an easy to run script it doesn't do
        anything.  Can someone please point me in the right direction?         Thank you.

        #!/bin/bash
        echo "What is the filename you want to grep?";
        read $i;
        OUTPUT="$(egrep '\b[A-Z]+| [0-9:]+ \b' $i | grep -v anaged |
        grep -v No| sort -u | grep -v Na | grep -v Ser | grep -v Ty |
        grep -v Contact)";
        echo "${OUTPUT}";
        done


Later,
JP
-- -------------------------------------------------------------------
JP Vossen, CISSP | http://www.jpsdomain.org/ | http://bashcookbook.com/
___________________________________________________________________________
Philadelphia Linux Users Group         -- http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  -- http://lists.phillylinux.org/mailman/listinfo/plug

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug