How to execute a bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to execute a bash script
# 1  
Old 12-06-2010
How to execute a bash script

Hi All,

I am trying to write a simple bash script.

Code:
rm -f File1

I saved this as test.sh
Now when I want to execute it, I type
Code:
./test.sh

It gives me error "command not found"

What I am doing incorrect here? Do I have to add anything in script like
Code:
#!/bin/bash

# 2  
Old 12-06-2010
Yes, you should add "#!/bin/bash" as the first line.

You also need to set the script executable.

Code:
chmod +x test.sh

And, you should never, ever name a script "test" because there's already system utilities named "test".
# 3  
Old 12-06-2010
Thanks for your reply.
I did as mentioned by you.

Now my script looks like:

Code:
#!/bin/bash
rm -f File1

I renamed it to Trial12.sh
I also changed mode by using
Code:
chmod +x Trial12.sh

And I executed using
Code:
 ./Trial12.sh

I got this error:
-bash: ./Trial12.sh: /bin/bash^M: bad interpreter: No such file or directory
# 4  
Old 12-06-2010
Stop editing your scripts with Notepad. Windows saves carriage returns to the end of lines, breaking everything. Use a real editor inside your terminal, like nano or vim.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 12-06-2010
Thanks!
Actually I am new to Unix scripting and not very comfortable with Vi editors.
Now I did everything in unix and it is working.

Thanks Again.
# 6  
Old 12-10-2010
Quote:
Originally Posted by palak08
Thanks!
Actually I am new to Unix scripting and not very comfortable with Vi editors.
vi was designed for an ancient, minimal, and very alien keyboard, and emacs was designed for something even weirder... I've been scripting for years and still dont' use vi unless I have to. It's useful to learn since lots of unix systems have one or the other and nothing else, but if you install nano, I think you'll find it a lot more familiar.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying bash script to take each line in a file and execute command

I need to modify a bash script to to take each line in a file and execute command. I currently have this: #!/bin/bash if ; then echo "Lipsa IP"; exit; fi i=1 ip=$1 while ; do if ; then rand=`head -$i pass_file | tail -1` user=`echo $rand | awk '{print $1}'` pass=`echo $rand | awk '{print $2}'`... (3 Replies)
Discussion started by: galford
3 Replies

2. Programming

CGI Perl script to execute bash script- unable to create folder

Hi I have a bash script which takes parameters sh /tmp/gdg.sh -b BASE-NAME -n 1 -s /source/data -p /dest/data/archive -m ARC gdg.sh will scan the /source/data and will move the contents to /dest/data/archive after passing through some filters. Its working superb from bash I have... (0 Replies)
Discussion started by: rakeshkumar
0 Replies

3. Shell Programming and Scripting

bash script to execute a command remote servers using ssh

Hello, I am running into few issues, please suggest me what I am missing. I am running this script on a linux host. Main idea of this script is to, login to each host via ssh and get uid of user, service user that I trying to run this script, has already deployed ssh keys and provide sudo... (8 Replies)
Discussion started by: bobby320
8 Replies

4. UNIX for Dummies Questions & Answers

Bash script to execute a program to rename files

I just can't figure it out , so please just give me a pice of advise how to: The existing Linux program foo2bar takes as its only argument the name of a single foo file and converts it to an appropriately-named bar file. Provide a script that when executed will run foo2bar against all foo... (4 Replies)
Discussion started by: raymen
4 Replies

5. Shell Programming and Scripting

Execute ssh commands through bash script

Hi all! I am trying to write a script that will check if a certain directory is available at several different nodes and then do stuff in it ..... On the beginning of the script I give as a variable the directory and the number of the nodes and then I loop like this: for... (3 Replies)
Discussion started by: idet2
3 Replies

6. Shell Programming and Scripting

Execute commands from script in current bash session

I have a file as follows: cat /etc/mxg/ssh-hostsmx.example1.com.au:2225 mx2.example2.com.au:2225 mx.example3.com.au:2225 mail.example4.com.au:2225 mail.example5.org.au:2225 mail.example6.com.au:2225I want to dynamically create aliases for quick access to these servers from bash. I wrote... (4 Replies)
Discussion started by: jelloir
4 Replies

7. Shell Programming and Scripting

bash script won't execute (Mac)

I can't get any bash scripts to run in Terminal (Mac - Snow Leopard). I have the following super-simple script, and I can't get it to execute despite having the correct permissions (I think). #!/bin/bash echo "WORK... PLEASE?!" I named the file 'testScript.sh', and I added execution... (6 Replies)
Discussion started by: compulsiveguile
6 Replies

8. Shell Programming and Scripting

Execute interactive bash menu script in browser with PHP

I have an interactive menu script written in bash and I would like use PHP to open the interactive bash menu in a browser. Is this possible? Using the sytem() function in php runs the script but it's all garbled. Seems like maybe a terminal window needs to be opened in php first? ... (1 Reply)
Discussion started by: nck
1 Replies

9. UNIX for Dummies Questions & Answers

how to execute sh script in bash shell via crontab

hello. we are porting over from HPUX Shell to Linux. my default shell is bash so i can no longer schedule to execute a sh script in crontab. can anyone pls help me out? I searched the site but didnt find any details. thanks! (1 Reply)
Discussion started by: jigarlakhani
1 Replies

10. Shell Programming and Scripting

loop does not execute in bash script?

I have a very basic bash shell script, which has many "while... done; for .... done" loop clauses, like the following ~~ #!/bin/bash while blablalba; do .... done < /tmp/file for line in `cat blablabla`; do grep $line /tmp/raw ; done > /tmp/1; while blablalba2; do .... done <... (2 Replies)
Discussion started by: fedora
2 Replies
Login or Register to Ask a Question