0

I'm relatively novice in Unix Shell Scripting. How can I run the (following) multiple UNIX commands (put into a script, say "discover.sh", using my_log.txt input-file just once? Eventually, I would like to create an alias [alias discover1='~/discover.sh'] in my .bashrc.

Like:

$ discover1 my_log.txt

Current script:

/bin/egrep 'Version:|Online \(warning\)|Failed \(offline\)' my_log.txt;
/bin/grep -A7 "syscontrol realmmgr" my_log.txt;
/bin/grep -C2 BIOS my_log.txt;
1
  • Why would you want that as an alias instead of creating a shell script in your $HOME/bin directory, which should be on your $PATH? There probably are ways to cat the file just once, especially in bash. I'm not sure whether they're worth using. For example: cat my_log.txt | tee >(/bin/egrep 'Version...') >(/bin/grep -A7 "...") | /bin/grep -C BIOS. This loses control over the sequencing of the output (you could get partial lines from different processes). If you really want to scan the file just once, I suggest using Perl do the searches all at once. Not quite trivial, but pretty easy. Commented Apr 11, 2013 at 5:46

1 Answer 1

1

discover.sh should contain:

#!/bin/bash
/bin/egrep 'Version:|Online \(warning\)|Failed \(offline\)' "$1"
/bin/grep -A7 "syscontrol realmmgr" "$1";
/bin/grep -C2 BIOS "$1";

The variable $1 is automatically set to the first parameter of the script.

Sign up to request clarification or add additional context in comments.

2 Comments

Any reason not to use "$@" in place of "$1"? It's more general if you process any number of files (other than zero). It isn't entirely clear that it is safe if there is no argument; arguably, the script should check that there is at least a "$1" to work on.
These are both reasonable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.