LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Why does this simple script is not working? (https://www.linuxquestions.org/questions/linux-newbie-8/why-does-this-simple-script-is-not-working-4175414222/)

ismaelvc 06-30-2012 09:48 PM

Why does this simple script is not working?
 
Hi everyone!

I have a doubt, there is this simple script I made in arch, using nano, It worked perfectly the first time I made it, then I reinstalled my arch system, and now it doesn't work, I don't get what I am doing wrong, since I did exactly the same than the last time:

Code:

ismaelvc@toybox ~ $ nano now 

(this are the contents)
----------                                                                                                                                                                                     
#!bin/bash                                                                                                       
                                                                                                                 
# NOW!                                                                                                           
                                                                                                                 
echo "" &&                                                                                                       
date &&                                                                                                         
echo "" &&                                                                                                       
cal -3                                                                                                           
----------                                                                                                       
                                                                                                                 
ismaelvc@toybox ~ $ ls -l now                                                                                   
-rw-r--r-- 1 ismaelvc users 58 jun 30 21:23 now                                                                 
                                                                                                                 
ismaelvc@toybox ~ $ chmod +x now                                                                                 
                                                                                                                 
ismaelvc@toybox ~ $ ls -l now                                                                                   
-rwxr-xr-x 1 ismaelvc users 58 jun 30 21:23 now                                                                 
                                                                                                                 
ismaelvc@toybox ~ $ sudo mv now /usr/bin

ismaelvc@toybox /usr/bin $ file now
now: ASCII text

ismaelvc@toybox /usr/bin $ LANGUAGE=en now
bash: /usr/bin/now: bin/bash: bad interpreter: No such file or directory

ismaelvc@toybox /usr/bin $ sh now

sáb jun 30 21:19:02 CDT 2012

    mayo de 2012          junio de 2012        julio de 2012 
do lu ma mi ju vi sá  do lu ma mi ju vi sá  do lu ma mi ju vi sá
      1  2  3  4  5                  1  2  1  2  3  4  5  6  7
 6  7  8  9 10 11 12  3  4  5  6  7  8  9  8  9 10 11 12 13 14
13 14 15 16 17 18 19  10 11 12 13 14 15 16  15 16 17 18 19 20 21
20 21 22 23 24 25 26  17 18 19 20 21 22 23  22 23 24 25 26 27 28
27 28 29 30 31        24 25 26 27 28 29 30  29 30 31

As you can see it works if I use "sh", when I use chmod, it changes to a green color, meaning its executable, but file doesn't recognize it as a shell script, what to do, I have studied it various times, and I still dont get what I'm doing wrong!

thanks in advance.

schneidz 06-30-2012 09:59 PM

the first line: #!bin/bash should be #!/bin/bash

suicidaleggroll 06-30-2012 10:00 PM

You need a "/" in front of the "bin/bash" at the beginning of your script. IE:
Code:

#!bin/bash
should be
Code:

#!/bin/bash
edit: too slow....

ismaelvc 06-30-2012 10:05 PM

@schneidz & suicidaleggroll:

lol thank you very much, how silly of me, but I honestly didn't notice that.

greetings.

David the H. 07-01-2012 11:25 AM

By the way, the "&&"s (the "and" list operator) after each command aren't really necessary here. It's effect is to link two commands together so that the trailing command only executes if the previous one exits successfully.

But all of the commands you are using are so simple and stable that they will almost never fail, and there's no harmful effect involved even if they do. Just put one command on each line and save the list operators for when you really need them.

You also don't need the "" quotes after the echo commands, as it defaults to printing a newline anyway.

ismaelvc 07-01-2012 06:28 PM

@David the H.: Thank you very much!

Yes I have been wondering what is the difference between ";" and "&&", but I didn't know I could do it without any of them, also thanks for pointing out "echo" default behaviour, I think I need to actually read some man pages and not just "view" them! But there are just massive ammounts of linux information out there, sometimes, I get lost, or worse confused lol! :)

David the H. 07-03-2012 08:28 AM

Glad to help.

";", BTW, is just the default sequencer, acting the same as a newline. When one command terminates, whatever the status, the next one is executed. It's mostly there for combining multiple commands on a single line.


As for learning scripting, and Linux use in general, it just takes time. Like all other skills it becomes easier with practice, study and experience. I was just like you 10 years ago. You actually have it easier than I did; there are better resources available now.

Here are a few useful bash scripting references:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://wiki.bash-hackers.org/scripting/newbie_traps
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start
http://ss64.com/bash/

I particularly recommend the BashGuide, as it covers all the basics of bash scripting in a few, easy-to-read pages, along with links to lots of other helpful advice.


All times are GMT -5. The time now is 08:49 PM.