Solved

Issue with Linux Bash Script function command

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

809 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