Wednesday, September 14, 2011

What is the command in unix to get files to execute. For instance a file that prompts the user for his name.?

I have a file I created in vi editor.

In it it like



echo %26quot;Enter your class name%26quot;



read cname



echo %26quot;Enter class number



read cnumber



How do I actually get this to execute? what is the command?

And also do I have to change the file name to something like Example.sh? I know in perl the files must have .pl after the name.What is the command in unix to get files to execute. For instance a file that prompts the user for his name.?There's two things you need to do. Firstly the file needs execute permissions. Use 'chmod a+x filename' to enable them.



Secondly, your shell needs to know what command to use to execute the file. This is done by putting a line right at the start of the file starting with #! and followed by the path to the command to use.



In your case, it looks like a shell script so you'd use something like the following as the first line:



#!/bin/sh



The file extension doesn't actually matter, it's just to give you a better idea of what kind of script it is when looking at the filename. For example a perl script doesn't strictly need to have .pl after the name, it just needs a #!/usr/bin/perl (or whatever the path to the perl executable is on your system) as the first line.



Once you've done all that, you should be able to just type the script's name to run it, assuming it's in your path. For security reasons, on some systems you may need to use ./scriptname if the script's in your current path.

No comments:

Post a Comment