Solved

Issue with Linux Bash Script function command

Posted on 2013-11-27
3
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 39681657
try to name the function other than test since test is acommand
0
 

Author Comment

by:Tbalz
ID: 39681666
That didn't work but thanks
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39681674
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

737 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