Linux commands in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Linux commands in shell script?
# 1  
Old 09-20-2011
Linux commands in shell script?

I am trying to learn to write basic shell scripts. I have a little experience with perl but none with shell. I am trying to write a simple script to write the last 15 or so lines of log files for my web server to a temp file so I can view all at once. Here's what I have. Are you not able to use commands like tail or am I going about it wrong. I looked at some other scripts that used similar commands in a script the same way.

Code:
#!/bin/bash


TEMPFILE="`mktemp -t web_info.XXXXXX`" || exit 1


HTTPNET="tail -n 15 /var/log/httpd/access_log:tail -n 15 /var/log/httpd/error_log:tail -n 15 /var/log/httpd/ssl_access_log:tail -n 15 /var/log/httpd/ssl_error_log:tail -n 15 /var/log/httpd/ssl_request_log"
LOGNET="tail -n 15 /var/log/secure:tail -n 5 /var/log/firewall:tail -n 15 /var/log/messages"

PRG="$HTTPNET:$LOGNET"


echo "$PRG" > $TEMPFILE 2>&1

output:

tail -n 15 /var/log/httpd/access_log:tail -n 15 /var/log/httpd/error_log:tail -n 15 /var/log/httpd/ssl_access_log:tail -n 15 /var/log/httpd/ssl_error_log:tail -n 15 /var/log/httpd/ssl_request_log:tail -n 15 /var/log/secure:tail -n 5 /var/log/firewall:tail -n 15 /var/log/messages

It's just echoing the commands, what am I missing?

Thanks.
# 2  
Old 09-20-2011
Use semi-colon(;) between commands not colon(:)

The reason it's just echoing your command is that's what you asked for, the echo command just outputs strings to stdout eg echo "Hello, World"
Code:
echo "$PRG" > $TEMPFILE 2>&1

Try

Code:
$PRG > $TEMPFILE 2>&1


Last edited by Chubler_XL; 09-20-2011 at 08:41 PM..
# 3  
Old 09-20-2011
Looks like you are trying to dynamically construct a script and execute it... if so:

Code:
#!/bin/bash

TEMPFILE=$(mktemp -t web_info.XXXXXX) || exit 1
# Note the semicolon as command separator... not colon..
HTTPNET='tail -n 15 /var/log/httpd/access_log;tail -n 15 /var/log/httpd/error_log;tail -n 15 /var/log/httpd/ssl_access_log;tail -n 15 /var/log/httpd/ssl_error_log;tail -n 15 /var/log/httpd/ssl_request_log'
LOGNET='tail -n 15 /var/log/secure;tail -n 5 /var/log/firewall;tail -n 15 /var/log/messages'

PRG="$HTTPNET;$LOGNET"

echo "$PRG" | bash >$TEMPFILE 2>&1

That should "work"... but not sure if it's really what you are wanting or not.
# 4  
Old 09-20-2011
I forgot about the quotes too. Double quotes print exactly what in them, correct? The single allow me to define what to do?

cj- That is exactly what I wanted to do. Thanks much!

chubler- I made the semicolon changes and single quotes around my commands. The script now prints only the last command tail -n 15 /var/log/messages but puts a nice seperator tag. Why is it not picking up the other files? Compared to cj's code the only difference is the output to file at the end. Here's the new code and output. Thanks!

Code:
#!/bin/bash


TEMPFILE=$(mktemp -t web_info.XXXXXX) || exit 1
# Note the semicolon as command separator... not colon..
HTTPNET='tail -n 15 /var/log/httpd/access_log;tail -n 15  /var/log/httpd/error_log;tail -n 15 /var/log/httpd/ssl_access_log;tail  -n 15 /var/log/httpd/ssl_error_log;tail -n 15  /var/log/httpd/ssl_request_log'
LOGNET='tail -n 15 /var/log/secure;tail -n 5 /var/log/firewall;tail -n 15 /var/log/messages'

PRG="$HTTPNET;$LOGNET"

$PRG > $TEMPFILE 2>&1

OUTPUT:
Code:
tail: cannot open `/var/log/httpd/access_log;tail' for reading: No such file or directory
tail: cannot open `/var/log/httpd/error_log;tail' for reading: No such file or directory
tail: cannot open `/var/log/httpd/ssl_access_log;tail' for reading: No such file or directory
tail: cannot open `/var/log/httpd/ssl_error_log;tail' for reading: No such file or directory
tail: cannot open `/var/log/httpd/ssl_request_log;tail' for reading: No such file or directory
tail: cannot open `/var/log/secure;tail' for reading: No such file or directory
tail: cannot open `/var/log/firewall;tail' for reading: No such file or directory
==> /var/log/messages <==
Sep 18 20:41:38 localhost yum: Installed: 1:java-1.6.0-openjdk-1.6.0.0-1.31.b17.el6_0.i686
Sep 18 21:39:33 localhost avahi-daemon[1691]: Received packet from invalid interface.
Sep 18 21:39:34 localhost avahi-daemon[1691]: Received packet from invalid interface.
Sep 18 21:40:51 localhost avahi-daemon[1691]: Received packet from invalid interface.
Sep 18 21:40:52 localhost avahi-daemon[1691]: Received packet from invalid interface.
Sep 18 21:41:12 localhost avahi-daemon[1691]: Received packet from invalid interface.
Sep 18 21:41:13 localhost avahi-daemon[1691]: Received packet from invalid interface.
Sep 18 21:42:17 localhost avahi-daemon[1691]: Received packet from invalid interface.
Sep 18 21:42:18 localhost avahi-daemon[1691]: Received packet from invalid interface.
Sep 19 09:05:07 localhost avahi-daemon[1691]: Received packet from invalid interface.
Sep 19 09:05:07 localhost avahi-daemon[1691]: Received packet from invalid interface.
Sep 19 19:31:09 localhost yum: Erased: subsonic
Sep 19 20:24:58 localhost kernel: npviewer.bin[4763]: segfault at 218 ip  0106c2d6 sp bff9b028 error 6 in libflashplayer.so[dee000+bc4000]
Sep 20 06:33:39 localhost kernel: ip_tables: (C) 2000-2006 Netfilter Core Team
Sep 20 06:33:39 localhost kernel: nf_conntrack version 0.5.0 (7993 buckets, 31972 max)

# 5  
Old 09-20-2011
The issue is to do with how the shell does expansion/subsitution and word splitting.

Only brace expansion, word splitting, and filename expansion can change the number of words of the expansion; other expansions expand a single word to a single word

To work around this issue either pipe commands to another shell like cjcox did or use eval:
Code:
eval $PRG > $TEMPFILE 2>&1

# 6  
Old 09-20-2011
Got it. Thanks. What caused the header to show up and I have been struggling to get it to show before all of the files. In fact now that i wokrs correctly and displays all log file info, it doesn't show at all.
Code:
==> /var/log/messages <== 
Sep 18 20:41:38 localhost yum: Installed: 1:java-1.6.0-openjdk-1.6.0.0-1.31.b17.el6_0.i686 
Sep 18 21:39:33 localhost avahi-daemon[1691]: Received packet from invalid interface. 
Sep 18 21:39:34 localhost avahi-daemon[1691]: Received packet from invalid interface. 
Sep 18 21:40:51 localhost avahi-daemon[1691]: Received packet from invalid interface. 
Sep 18 21:40:52 localhost avahi-daemon[1691]: Received packet from invalid interface.

With the eval it works as I wanted. I guess I am probably better off switching to perl if I want to be more detailed like adding headers and dating the file. Thanks again for the quick response and explainations so I have something to read more about.
# 7  
Old 09-21-2011
Tail puts the filename as a header if you pass more than 1 file to it at once, so
Code:
$ tail a b c
==> a <==
one
==> b <==
two
==> c <==
three

and
Code:
$ tail a; tail b; tail c
one
two
three

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Two exec commands in one shell script?

Hi Folks - Is there a way to add two execs to one script? For instance, I need to redirect the stdout and stderr to two separate directories. I want to do this: #::-- Direct STDOUT and STDERROR to repositories --::# exec 2>"${_ERRORFILE}" > "${_LOGFILE}" exec 2>"/new/path/file.err" >... (7 Replies)
Discussion started by: SIMMS7400
7 Replies

2. UNIX for Dummies Questions & Answers

Commands to run from shell script

Hi script> isumid 98765432 if i give above command in cmd prompt it is running the same thing if i give inside the shell script it is not working below is the code #!/bin/bash isumid 98765432 please give me a solution (16 Replies)
Discussion started by: Ramrangasamy
16 Replies

3. Shell Programming and Scripting

Using Ex editor commands in a shell script - Help!

Hi all, I am trying to use the Ex editor and its commands in a script - more specifically within an if statement within a while loop. Here are the basics of the loop: cat $file1 | while read line do grep $line $file2 if ] then echo $line > $file2 elseex $file2 /ESI185... (4 Replies)
Discussion started by: luke222010
4 Replies

4. UNIX for Dummies Questions & Answers

How to use a Shell Script for specific commands

I have a set of commands that I am using to copy specific areas of source code in a Cold Fusion document and export the code into recreations of the original files into a sub-directory. Here are the 3 commands I am using: mkdir ./out && for x in *.cfm; do awk '{FS="<!-- InstanceBeginEditable... (17 Replies)
Discussion started by: dratech09
17 Replies

5. Shell Programming and Scripting

writing a shell script of commands

Can anyone help me out in visualizing on what is the logic behind simple unix commands. For Eg: ls command lists files and directories, how it displays I need to know the source code for commands like this. (1 Reply)
Discussion started by: rahul_11d
1 Replies

6. Shell Programming and Scripting

Why can't embed commands like fg or bg in a shell script ?

Hi Can someone explain in an easy way that why can't embed commands like fg or bg in a shell script ? (4 Replies)
Discussion started by: qiulang
4 Replies

7. Shell Programming and Scripting

How to get ascii value using shell commands/script

Hi gurus, Need command/code to get the ASCII value for a character say 65 for A and vice versa in shell script. Thanks in advance (2 Replies)
Discussion started by: kittu1979
2 Replies

8. Shell Programming and Scripting

how to sql commands in shell script

hi, plz let me know one example in using sql command in shell script thanks inadvance -bali. (1 Reply)
Discussion started by: balireddy_77
1 Replies

9. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question