Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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\""
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.
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>
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.
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).
[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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.