Cable Forum

Cable Forum (https://www.cableforum.uk/board/index.php)
-   General IT Discussion (https://www.cableforum.uk/board/forumdisplay.php?f=19)
-   -   $ bash myscript (https://www.cableforum.uk/board/showthread.php?t=33602842)

altis 30-10-2006 12:00

$ bash myscript
 
I'm having problems running scripts on CentOS 4.4.

I have a script file called wait.txt that looks like this:
Code:

sleep 10s
echo "Hello World"

All very simple!

But when I try to run the thing I get this:
Code:

[root@localhost asterisk]# cat wait.txt
sleep 10s
echo "Hello World"[root@localhost asterisk]# bash wait.txt
'leep: invalid time interval `10s
Try `sleep --help' for more information.
Hello World
[root@localhost asterisk]#

Eh!

gazzae 30-10-2006 12:04

Re: $ bash myscript
 
Try taking the s out so the line is just

Sleep 10

altis 30-10-2006 12:13

Re: $ bash myscript
 
Code:

[root@localhost asterisk]# cat wait.txt
sleep 10
echo "Hello World"[root@localhost asterisk]# bash wait.txt
'leep: invalid time interval `10
Try `sleep --help' for more information.
Hello World
[root@localhost asterisk]#


The Jackal 30-10-2006 12:23

Re: $ bash myscript
 
COME ON BOYS stop dissing me ....

wait.sh

------------------------------------------
#!/bin/sh

echo "Hello World"
echo "Going to sleep"
sleep 3;
echo "Wakey wakey"
------------------------------------------

chmod 755 wait.sh

./wait.sh ( to execute)

---------------------
EDIT
Notice the first line of the script ?
#!/bin/sh

?

That tells the OS that this is a shell script...

If I were to change this line to #!/usr/bin/perl
it would become a perl script

altis 30-10-2006 12:40

Re: $ bash myscript
 
I don't know why Linux peeps like to make life so hard for themselves. There are at least two ways to run a script. As you say, you can add '#!/usr/bin... etc' at the front, then chmod it to make it executable and the run it with './scriptname'. Alternatively, create your script file, don't bother to add that funny first line, don't bother to chmod it then just run it with 'bash scriptname'.

Anyway, I added the semicolon at the end of the 'sleep' line and it's a bit better. At least it sleeps now!
Code:

[root@localhost asterisk]# cat wait.txt
sleep 10;
echo "Hello World"[root@localhost asterisk]# bash wait.txt
: command not found
Hello World
[root@localhost asterisk]#


The Jackal 30-10-2006 13:13

Re: $ bash myscript
 
Quote:

Originally Posted by altis (Post 34147207)
I don't know why Linux peeps like to make life so hard for themselves. There are at least two ways to run a script.

Heh
There are two types of ppl in this world my friend :

Those with guns

And those who dig.

----
Just pointing out the good the bad and the ugly wrt scripting.

Once you have hundreds of these scripts and want one to execute one with another and return the resultsets then you'll realise pretty sharpish if only i had put the stupid line at the start of my scripts i wouldn't be in this pickle.

Good programming practice too : shudders at the thought of code in a txt file : code in a text file what type of code is it ?

file <filename> would tell me straight away
file <yourfile> would say text file

More importantly how do you execute <yourfile> what if I use a different interpreter to bash say tclsh and in inadvertently a safe command in bash becomes an 'rm-rf /' in tclsh.

Your choice have fun coding.

---------- Post added at 12:13 ---------- Previous post was at 12:05 ----------

PS I'm not having a moan here just pointing out some best practices afterall I think its great you're doing some scripting :)


All times are GMT +1. The time now is 08:12.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
All Posts and Content are © Cable Forum