Function command execution from root


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function command execution from root
# 1  
Old 10-09-2013
Function command execution from root

I have a function hello, that is echoing i have put that function in .bash1 file then recalling the function with same user but with su command but it is not working.

username -> test
function -> below function save in .bash1
Code:
function hello() {
   echo "Hello, $1!"
}
export -f hello

I run the below command from root
Code:
# su - test -c /home/test/.bash1 ; hello

Above command giving me error
Code:
-bash: hello: command not found

Note: What should i do that so my .bash1 function will run from root with su command, if it is not possible then what else the solution.

Last edited by Scott; 10-09-2013 at 09:19 AM.. Reason: Fixed code tags
# 2  
Old 10-09-2013
Exporting the function won't help, since you're calling it from its parents shell.

You will need to source the file, and quote the commands:

Code:
# su - test -c ". /home/test/.bash1; hello"

This User Gave Thanks to Scott For This Post:
# 3  
Old 10-09-2013
Thanks so much, now i understand the logic.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable gets auto updated after function execution

Hi Team In the below code, irrespective of the if statement that gets executed, retcd is being assigned a standard value(1) instead of changing as per code. Could you please help to see where is it going wrong. rval=0 CONF_FILE=/apps/wmroot/scripts/props/UMPath.properties NOHUP="nohup"... (3 Replies)
Discussion started by: harishshankar
3 Replies

2. UNIX for Dummies Questions & Answers

Execution of command at command prompt

Hello Experts, I am still learning linux and have come across a question, hope to get some answer. I have two servers, and both have the same version of svn client installed and both have the same user_id. my SVN client version: svn, version 1.6.11 (r934486) compiled Mar 2 2011,... (4 Replies)
Discussion started by: babyPen1985
4 Replies

3. Shell Programming and Scripting

Sequential Function Execution

I am having two different function in my script. When control is at first function I do not want to execute another function. How I can do that? Help is highly appreiated as I am not sure How I can do it in Unix? Thanks, Vikram. (2 Replies)
Discussion started by: VSom007
2 Replies

4. Shell Programming and Scripting

Command execution from other directory

Hi, I need to execute some commands in a directory. script called test.sh from /usr/bin/ and the commands which are there in the script needs to be executed from /etc/ and come back to another directory /lib/ and execute some commands which are there in the script. Hope you understand... (1 Reply)
Discussion started by: munna_dude
1 Replies

5. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Want to terminate command execution when string found in the command output

Hi Experts, I am very much new to linux scripting, I am currently working on reducing my manual work and hence writing a script to automate few task. I am running below command to snmpwalk the router.. snmpwalk -v 3 -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv... (19 Replies)
Discussion started by: Hanumant.madane
19 Replies

7. Solaris

get the total execution of command

hi, i have to get the info between start to stop of a command execution suppose, if we execute the command like pkgadd then while its executing it ask some questions , i have to get total information untill pkgadd command complete into a perticular file. (3 Replies)
Discussion started by: shankr3
3 Replies

8. Shell Programming and Scripting

Code for execution command by command

a) cd /root/user/dir/sd/disk ./runInstaller -silent -responseFile b) cd /root1/user1 cp /root/user/dir/sd/disk/ram.txt now a) executes and starts running but b) interupts a) and is executed while a) is running on the other hand so I want b) to start only after successfull completion of... (6 Replies)
Discussion started by: sriki32
6 Replies

9. UNIX for Advanced & Expert Users

command execution alert

hi Guys! My requirement is... I need to get notified if somebody executes a specific commands...like kill or httpd stop.... something like that.... can somebody help me out... Regards, kiran (8 Replies)
Discussion started by: dddkiran
8 Replies

10. UNIX for Advanced & Expert Users

command execution ??

hi i have small shell script as follows cd /utilities/promoter/tmp grep SENDREPLY $1 | grep 'zzzzz@zzz.com' | awk -F" -f1 > /tmp/$LOGNAME/$1.request cd /tmp/$LOGNAME grep -e "\.sql" -e "\.md" $1.request > upd_$1.txt grep -v -e "\.sql" -e "\.md" $1.request > copy_$1.txt ... (1 Reply)
Discussion started by: zedex
1 Replies
Login or Register to Ask a Question