Solved

Issue with Linux Bash Script function command

Posted on 2013-11-27
3
673 Views
Last Modified: 2013-11-27
Hi Guys,

I'm having an issue with my Bash script where I want to create a function called test and run commands using that function so would not have to keep writing it over and over. Can someone tell me how I can fix my script below to do that? I'm also trying to display either the Green OK when the statement has successfully completed and Red Failed when it has not. I also would like the output of the entire script to be sent to a custom made log file. Does anyone know how i can accomplish all of this?

Thanks so much!

#!/bin/bash

. /etc/init.d/functions

function test {
"$@"
if [ $? == 0 ]
then
     echo [ "The File is there" ] && echo_success
else
     echo "The File is not there"
fi
}

test mount | grep "on /tmp" > /dev/null 2>&1
test mount | grep "on /var" > /dev/null 2>&1

Open in new window

0
Comment
Question by:Tbalz
3 Comments
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
try to name the function other than test since test is acommand
0
 

Author Comment

by:Tbalz
Comment Utility
That didn't work but thanks
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
1) You must use

eval "$@"

in line 6 so the shell will "evaluate" the command string (i.e. execute it as a whole instead of resolving things like | or > prematurely).

2) You must enclose the string passed to the function in quotes (best use single quotes) so that it gets passed to the function "as is" (i.e. things like |, > are evaluated inside the function by "eval" and not by the shell before passing the parameter to the function).

test 'mount | grep "on /tmp" > /dev/null 2>&1'

3) Avoid naming a function "test", because it's a shell builtin, although "bash" will tolerate it (functions take precedence over builtins), but e.g. "ksh" doesn't tolerate it.

As for the colours: You could use ANSI colour escape codes:
http://bluesock.org/~willg/dev/ansi.html
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now