Solved

Issue with Linux Bash Script function command

Posted on 2013-11-27
3
684 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

816 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

8 Experts available now in Live!

Get 1:1 Help Now