Hi

I am a novice in Shell Scripting but wanna learn it as soon as possible as it is such a powerful language.

I already have a script created(by a senior fellow) and I can run the .sh file simply by command ./xxxx.sh
Now I have created my own script with my name yyyy.sh but I am not able to run it with the command ./yyyy.sh and I get the following error :

./yyyy.sh: Command not found.

I have also set the rights as 777 still I can't run it.

Am I messing up with the path?

Please guide me as it would be really simple for you guys to figure out.

Recommended Answers

All 10 Replies

Hmm I might be wrong but inst it chmod 775 ?

In addition to the executable permissions, you need the shbang first line: #!/bin/sh

try by adding all these option
step 1:
first know where is shell interpreter by type in command prompt which bash (which is command ) is show the you interpreter
step 2:
add #! /step 1 output at the first line
step 3:
after save and exit from script
execute this command chmod +x your script name
step 4:
then ./your script name

which bash gives me /usr/usc/bin/bash
and I use #!/bin/bash as my first line but it still doesn't work that way.

Do I need to do something specific for the first time when I execute a script, is there something like an executable in Unix?

The #!path-to-interpreter in the first line, plus setting the executable bits with chmod is the equivalent of an executable.
What follows the "#!" is the exact and full path to the interpreter. The bash interpreter is usually /bin/bash, so the line would usually be #!/bin/bash You can check that by chanting ls /bin/bash . If the response is "/bin/bash" then use the line as specified. If the response is "/bin/bash: No such file" or something similar, then chant which bash and put the response in the shbang line which would then look like #!/usr/usc/bin/bash

Hmm. For instance, do this:
create a file named 'tryme' in the current directory
in that file, place these lines:

#!/bin/sh
echo "This works"

now chant chmod +x ./tryme finally chant ./tryme which should print "This works"
If it fails, you will see one of two problems:

  1. You see Permission denied or equivalent: You didn't succeed with the chmod command
  2. You see bad interpreter or no such file or directory or similar: /bin/sh is not there (which would be very surprising)

Your own script should succeed or fail in a similar manner.

Hi,


I am kallin.Do I need to do something specific for the first time when I execute a script, is there something like an executable in Unix.


Thanks !

________________


<FAKE SIGNATURE>

Please re-read the posts above. I think they are correct and complete. However if you do not understand them, then:

There are three things that work together in a Unix-like OS to allow a file to be an executable. After each point, I will add something in italic that may help you understand.

  1. The file must have the 'execute' permission set. chant man chmod
    • If you do not have execute permission, the error is Permission denied
  2. If it is a shell script, it must have the 'shbang' first line. http://www.informit.com/articles/article.aspx?p=350778&seqNum=6
    • If there is no shbang line, the response is that nothing happens, quietly
    • If the shbang line does not match an interpreter, the error is bad interpreter or no such file or directory
    • If you mention an interpreter that cannot handle your script, the interpreter will give an error message
  3. It must be on your path, or you must specify an exact path. If tryme is a shell script file in your current directory you can chant ./tryme To see your path, chant echo $PATH
    • If you do not give a full path (such as ./tryme), the error is command not found.
    • If you give a full path that is incorrectly spelled, the error is no such file or directory
commented: Good effort! +9

Hi,
I was able to go through the above issue but I am stuck at a later point.
I am trying to capture system date in a variable.I want the date in mm/dd/yyyy format, and this is what I do:

TodayDate=$(date +"%m/%d/20%y")
echo "TodayDate is $TodayDate"

I am getting the following error:
temp1.sh: syntax error at line 61: `TodayDate=$' unexpected

I am not sure what's wrong. Please help.

The bourne shell (sh) does not support $(sub command) syntax. You have two options.
Best option: Use bash or at least ksh
If you must use sh: use back-quotes to get a sub command: todayDate=`date +%m/%d/%Y` Note: You don't need the quotes around the format string (it doesn't hurt, though), and as far as I know, capital Y gets you the four-digit year on all (recent) OSs.

It would not hurt to learn to use man (or info if your system has it). Although man and info pages are somewhat difficult, they seldom lie (its just that they tell the truth confusingly). Also, consider searching the web for "bourne shell introduction" or some similar strings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.