Problem in running a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in running a shell script
# 1  
Old 03-16-2010
Problem in running a shell script

Hi,

I'm trying to run a simple shell script whose contents are pasted below.

#!/usr/bin/ksh
echo $PATH
performbuild
{
echo "Inside performBuild function"
}
echo "Main Shell started"
performbuild
echo "Main Shell ended"

DV> ls -ltr
total 48
-rwxr-xr-x 1 pb5377 it-ibm 145 Mar 16 17:50 test_upd.sh
-rwxr-xr-x 1 pb5377 it-ibm 144 Mar 16 19:11 test1.sh
-rwxr-xr-x 1 pb5377 it-ibm 156 Mar 16 19:37 test.sh
DV> ./test.sh
ksh: ./test.sh: not found
DV>


If at all if the script gets executed, it is not recognizing the function and the commands in the script get executed sequentially.
The output looks like

DV> ./test.sh
/usr/bin:/sbin:/usr/sbin:/usr/bin:/usr/ccs/bin:/usr/xpg6/bin:/usr/xpg4/bin:/usr/ucb:/usr/local/bin:/usr/local/sbin:/usr/sfw/bin:/opt/VRTS/bin:/usr/local/bin:/usr/local/sbin:/usr/ccs/bin
./test.sh[5]: performbuild: not found
Inside performBuild function
Main Shell started
./test.sh[11]: performbuild: not found
Main Shell ended
DV>

Any help is appreciated

Thanks,
blp
# 2  
Old 03-17-2010
The general format for declaring a function is
Code:
function function_name()

So you need to write like
Code:
#!/usr/bin/ksh
echo $PATH
function performbuild()
{
        echo "Inside performBuild function"
}
echo "Main Shell started"
performbuild
echo "Main Shell ended"

The sample output I got is:
Code:
/home/thillaiselvan:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:.:/home/thillaiselvan/.vim/ctags-5.7/
Main Shell started
Inside performBuild function
Main Shell ended

# 3  
Old 03-17-2010
Code:
$ 
$ cat -n f5.ksh
     1  #!/bin/ksh
     2  echo "hello, world!"
     3  performbuild()
     4  {
     5  echo "Inside performBuild function"
     6  }
     7  echo "Main Shell started"
     8  performbuild
     9  echo "Main Shell ended"
$ 
$ ./f5.ksh
hello, world!
Main Shell started
Inside performBuild function
Main Shell ended
$ 
$

tyler_durden
# 4  
Old 03-17-2010
Thanks for the reply. I tried the same. But, I get an error

./test5.sh[4]: syntax error at line 3 : `(' unexpected .

Thanks,
blp
# 5  
Old 03-17-2010
Quote:
Originally Posted by blp_18
... I tried the same. But, I get an error

./test5.sh[4]: syntax error at line 3 : `(' unexpected .

...
What's the output of the following command on your system ?

Code:
which ksh

and what does your script look like ?

tyler_durden
# 6  
Old 03-17-2010
Thanks Tyler_Durden for your reply. I got it worked now. The problem is that some funky/control characters were came in with the file and they were causing the problem initially.

I tried this way

$ dos2unix <filename> > <destfile>

Thanks,
blp
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem running plsql using printf command on bash shell

I am running plsql using printf on a shell, but i am getting some strange error, can someone point what exactly am i missing, $ echo $SHELL /bin/bash $ printf " > SET serveroutput ON trimspool on feed off echo off > declare > p_val number; > d_val varchar2(10); > begin > SELECT... (1 Reply)
Discussion started by: kamauv234
1 Replies

2. UNIX for Beginners Questions & Answers

Problem running a C file through the shell

Hi, I have tried running c file with: gcc -o example1 first.c ./first.c But got : ./first.c: line 6: syntax error near unexpected token `!=0' ./first.c: line 6: `if( fork()!=0 )' This is the file: #include <sys/types.h> (9 Replies)
Discussion started by: uniran
9 Replies

3. Shell Programming and Scripting

Cp not working in shell script but running perfectly from shell

Dear All, I have script. Dest="" IFS=' ' for translation in $(echo $MY_MAP) do t1=$(echo $translation | cut -d"=" -f1) t2=$(echo $translation | cut -d"=" -f2| cut -d"," -f1) if then Dest=$UNX/$u_product_path/$u_study_path/$UNXTR/$t2 break; ... (4 Replies)
Discussion started by: yadavricky
4 Replies

4. Shell Programming and Scripting

Problem on running a script

Hi all, Running follow command on terminal; $ glance -T cloudlive -I ubuntu -K ubuntu123 -N \ http://127.0.0.1:5000/v2.0/OpenX add name="cirros" is_public=true \ container_format=ovf disk_format=raw < \ /srv/cirros-0.3.0-x86_64-disk.img It works without problem. Putting it on a... (5 Replies)
Discussion started by: satimis
5 Replies

5. Shell Programming and Scripting

Problem running a program/script in the background from a script

Hi all, I have a script that calls another program/script, xxx, to run in the background. Supposedly this program at most should finish within five (5) minutes so after five (5) minutes, I run some other steps to run the script into completion. My problem is sometimes the program takes... (5 Replies)
Discussion started by: newbie_01
5 Replies

6. UNIX for Dummies Questions & Answers

problem running script

hey, i'm trying to run several processes due 2 loops : the code is: cd ${TIBCO_HOME}/tra/domain/${DOMAIN}/application/ for app_name in ls * do echo $app_name if && && ; then exit else echo $app_name cd $app_name pwd for ps_name in ls *.sh do echo... (2 Replies)
Discussion started by: ohadr
2 Replies

7. Shell Programming and Scripting

problem running shell script (for oracle export) in crontab

Hello Gurus, I've been tasked with solving a problem at my new job and I'm stumped. We've got a script that dynamically builds an oracle export parameter files and then runs export from the shell. it runs fine when using the shell, but will NOT run (fails in one spot everytime) when entered... (1 Reply)
Discussion started by: jsheehan223
1 Replies

8. Solaris

Running from Shell Vs running from RC script

Hi, i have a script which need to do behave differently when run as a startup process from init.d/ rc2.d script and when run manually from shell. How do i distinguish whether my script is run by init process or by shell?? Will the command /proc/$$/psinfo | grep "myscript" work well???... (2 Replies)
Discussion started by: vickylife
2 Replies

9. UNIX for Advanced & Expert Users

Problem in running bash shell commands on HP-UX machine

Hello All, After login to the server we are explicitly calling /usr/local/bin/bash to activate bash shell properly. But since commands are not executing properly so I think it is not initialized well. I am facing following problems: 1) If I want to have a look on a particular file using tail... (6 Replies)
Discussion started by: abhishek0071
6 Replies

10. Shell Programming and Scripting

Problem in Running Script

Hi friends, i have written one small script named as "size.sh"..... while running its giving syntax error at line no 2: 'str=$' unexpected. How to run this file ??? please help me. file content is : ----------------------------- #!/bin/sh str=$( df -k | grep /usr/local)... (14 Replies)
Discussion started by: samfrance
14 Replies
Login or Register to Ask a Question