LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-26-2009, 01:46 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
How do i execute the file within vi .


Hi i m trying to execute my file from with in vi.
i m using following syntax
esc :!<program-name>
m i making any mistake as it is giving me an error.

/bin/bash: program2.sh: command not found

shell returned 127


My program is this:
#This program takes 3 Parameters from user namely name,address,phonenumber and store those details in detail.out file .
echo -n 'Please Enter Your Name: '
read name
echo -n 'Please Enter Your Address: '
read address
echo -n 'Please Enter Your Phone Number: '
read phoneno
#To store output in only milind directory by doing this if we run this file within any directory the file get stored in milind directory only.
echo $name > /root/milind/details.out
echo $address >>/root/milind/details.out
echo $phoneno >>/root/milind/details.out
#Two ways of displaying the output.
echo 'Output is stored to "details.out"'
echo "Output is stored to \"details.out\""
 
Old 10-26-2009, 01:57 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Can you run your script from the command prompt by simply typing its name as you have after the :! in vi?
 
Old 10-26-2009, 02:04 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Have you set the executable permission on?
 
Old 10-26-2009, 02:48 AM   #4
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Yes i have set the executable permissions.
when i execute sh programname.sh it works
 
Old 10-26-2009, 02:52 AM   #5
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by catkin View Post
Can you run your script from the command prompt by simply typing its name as you have after the :! in vi?
no
[root@test milind]# program2.sh
-bash: program2.sh: command not found
 
Old 10-26-2009, 03:25 AM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
In linux, the present working directory isn't in the path by default.

So you should probably do:
! `pwd`/<Program_name>

End
 
Old 10-26-2009, 03:28 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Either your script needs to be in one of the directories listed in $PATH (use echo $PATH to see what it is) or you must give the full path to your script. The full path must begin with "/" or "." (meaning the current directory) or ~ (meaning your home directory).

You can have "." in $PATH. It's convenient but not secure so is dangerous to use for root but probably OK for ordinary users.
 
Old 10-26-2009, 03:29 AM   #8
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by AnanthaP View Post
In linux, the present working directory isn't in the path by default.

So you should probably do:
! `pwd`/<Program_name>

End
not working however ! sh program-name.sh worked but i want to know the reason behind it.
 
Old 10-26-2009, 03:40 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by pinga123 View Post
not working however ! sh program-name.sh worked but i want to know the reason behind it.
Assuming you have execute permisssion on your script file, it may be that the shell tries to execute the file but does not know what to execute it with. Changing the first line to #!/bin/bash would solve that problem.

If it is not that then the issue is how the shell looks for executables, as explained above. When you use sh to run the script it has a different way of looking for executables compared with simply typing in the name of the shell script at the command prompt.

When you tried `pwd`/<program name>, did you use backquotes around pwd? That is ` or did you use single quotes, that is '

You could also try :!./<program name> That is the equivalent of `pwd`/<program name>
 
Old 10-26-2009, 03:51 AM   #10
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by catkin View Post
Assuming you have execute permisssion on your script file, it may be that the shell tries to execute the file but does not know what to execute it with. Changing the first line to #!/bin/bash would solve that problem.

If it is not that then the issue is how the shell looks for executables, as explained above. When you use sh to run the script it has a different way of looking for executables compared with simply typing in the name of the shell script at the command prompt.

When you tried `pwd`/<program name>, did you use backquotes around pwd? That is ` or did you use single quotes, that is '
However i can still be able to execute it using
! ./program-name.sh
You could also try :!./<program name> That is the equivalent of `pwd`/<program name>


I used ` and not ' .I also changed my first line to what you have suggested but still i m not able to run it.

How would i be able to execute it only by giving its name.

Last edited by pinga123; 10-26-2009 at 03:53 AM.
 
Old 10-26-2009, 03:52 AM   #11
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Any masked permissions in the current shell?

ie. whats the value of `umask`?

If the last character is zero, then it wont execute in the current shell but needs a sh invocation.

End
 
Old 10-26-2009, 04:07 AM   #12
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by AnanthaP View Post
Any masked permissions in the current shell?

ie. whats the value of `umask`?

If the last character is zero, then it wont execute in the current shell but needs a sh invocation.

End
umask
0027
 
Old 10-26-2009, 12:23 PM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by pinga123 View Post
I used ` and not ' .I also changed my first line to what you have suggested but still i m not able to run it.

How would i be able to execute it only by giving its name.
You can execute it by giving its name only by changing $PATH to include the directory containing program-name.sh. That will only work if you can run it using some of the methods already tried -- and it seems you cannot do that. Please start a terminal, cd to the directory containing program-name.sh, run the following commands and post the output
Code:
/bin/ls -l program-name.sh
head program-name.sh
echo $PATH
./program-name.sh
It's easier to read if you put it in code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).
 
Old 10-26-2009, 11:18 PM   #14
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
[root@test milind]# head program2.sh
#!/bin/bash
#This program takes 3 Parameters from user namely name,address,phonenumber and store those details in detail.out file .
echo -n 'Please Enter Your Name: '
read name
echo -n 'Please Enter Your Address: '
read address
echo -n 'Please Enter Your Phone Number: '
read phoneno
#To store output in only milind directory by doing this if we run this file within any directory the file get stored in milind directory only.
echo $name > /root/milind/details.out
[root@test milind]# echo $PATH
/bin:/usr/bin:/sbin:/usr/sbin:/root/.sysadmin/restart:/root/bin:/usr/bin/perl:/oracle/product/10.2.0/db_1/bin
[root@test milind]# ./program2.sh
Please Enter Your Name:
 
Old 10-26-2009, 11:19 PM   #15
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
How to add into path variable
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc will not execute, responds with 'no such file or directory' and 'no input file' nckeecho Ubuntu 9 07-24-2016 01:04 PM
how to execute a script file? Have file/directory not found error sirius57 Linux - Software 2 11-21-2007 11:43 PM
cannot execute binary file on a selfx file brucifer Linux - Software 4 03-21-2006 04:44 AM
execute .el file balloon Linux - Software 2 11-16-2004 06:02 AM
how to execute a file malik Linux - Software 1 06-10-2004 08:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration