Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Creating a file


  • Please log in to reply
8 replies to this topic

#1 Reddoug

Reddoug

  • Members
  • 113 posts
  • OFFLINE
  •  
  • Local time:06:08 PM

Posted 15 June 2010 - 07:55 PM

Hi All

I am try to learn some Unix/Linus programming. I am a beginner at it. I found a bash scripting tutorial. One of the steps says to create a file (first.sh). I am using Ubuntu 10.4 in terminal.

It says to Create a file (first.sh) as follows:

first.sh
#!/bin/sh
# This is a comment!
echo Hello World # This is a comment, too!

When I type first.sh I get an error message. Can not figure out why. In the tutorial it shows first.sh underlined. If I leave off fist.sh it will print out Hello World.

Thanks, Doug

BC AdBot (Login to Remove)

 


#2 Capn Easy

Capn Easy

  • Members
  • 597 posts
  • OFFLINE
  •  
  • Location:New Jersey
  • Local time:08:08 PM

Posted 15 June 2010 - 08:42 PM

If this is an online tutorial, it would be helpful to tell us where to find it.

Hi All

I am try to learn some Unix/Linus programming. I am a beginner at it. I found a bash scripting tutorial. One of the steps says to create a file (first.sh). I am using Ubuntu 10.4 in terminal.

It says to Create a file (first.sh) as follows:

first.sh
#!/bin/sh
# This is a comment!
echo Hello World # This is a comment, too!

When I type first.sh I get an error message. Can not figure out why. In the tutorial it shows first.sh underlined. If I leave off fist.sh it will print out Hello World.

Thanks, Doug


The first line, that I've highlighted in red, is telling bash (or whatever shell you might be running) to look for the executable named first.sh -- which hasn't been created yet.



There are several ways to create a file in UNIX/Linux. If it was me, I'd start up a text editor -- Ubuntu should have gedit -- and type in the lines:

#!/bin/sh
# This is a comment!
echo Hello World # This is a comment, too!


And then exit and save it as a file named first.sh. The resulting file should appear in your working directory, and should be executable by yourself (but not necessarily from any other account on your computer).


Bu-uuu-ut, without knowing what your tutorial is trying to teach, or how it's trying to get you there, there might be a reason to create the file differently.


Finally, I've been doing shell programming since the early 80s. Aside from being useful, it's also a lot of fun, and it can be a great way to learn how any computer works! Have fun!

#3 Brother Bill

Brother Bill

  • Members
  • 59 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Kansas City, MO
  • Local time:06:08 PM

Posted 15 June 2010 - 08:56 PM

I think the tutorial that he's using might be the Steve's Bourne / Bash shell scripting tutorial. At least the content displayed is the same.

#4 Reddoug

Reddoug
  • Topic Starter

  • Members
  • 113 posts
  • OFFLINE
  •  
  • Local time:06:08 PM

Posted 15 June 2010 - 09:01 PM

Thanks for the quick reply. I did not think about exiting and saving it as first.sh. That make sense.

Doug

#5 Brother Bill

Brother Bill

  • Members
  • 59 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Kansas City, MO
  • Local time:06:08 PM

Posted 15 June 2010 - 10:18 PM

In the tutorial it shows first.sh underlined. If I leave off fist.sh it will print out Hello World.


Hi Doug. Look closer at the tutorial. Where the first.sh is underlined, it is actually a hyperlink that will take you to a sample of the lesson if you click on it. Farther down the page (providing you're using the same tutorial I'm looking at), you'll find first2.sh also underlined. That is also a link leading to another sample. Hey, have fun with it.

#6 Andrew

Andrew

    Bleepin' Night Watchman


  • Moderator
  • 8,250 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:Right behind you
  • Local time:05:08 PM

Posted 16 June 2010 - 01:56 PM

In that particular tutorial, it seems that the parts that are inside the gray boxes are the actual code that's being discussed.

Tip: A general rule in shell scripts for Unix-like systems is that the first line always starts with a shebang ("#!") followed by the path to the shell interpreter you want to use (most often "/bin/sh" but can be any interpreter you like. Most Linux distros have /bin/sh pointing to /bin/bash anyway.)

#7 Reddoug

Reddoug
  • Topic Starter

  • Members
  • 113 posts
  • OFFLINE
  •  
  • Local time:06:08 PM

Posted 16 June 2010 - 08:16 PM

Thanks again for the info. I will check the links out. I guess what through me is when it said to create a file. I am taking a Unix programming class and Server 2008 class this fall. Like to get a jump on it.

Thanks again, Doug

#8 cryptodan

cryptodan

    Bleepin Madman


  • Members
  • 21,868 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Catonsville, Md
  • Local time:12:08 AM

Posted 18 June 2010 - 09:20 AM

Since you speak of using Unix and Linux, I would highly suggest you become familiar with the VI editor: Mastering the VI Editor.

Now with that said you do this:

username@hostname:$vi first.sh

when you enter vi you want to hit the letter "a" this will make it possible to enter the below text:

#!/bin/sh
# This is a comment!
echo Hello World # This is a comment, too!

You will want to also locate the file path to sh which is your shell.

You can do this via typing which sh, so your shell script knows where to look for the sh "shell" binary.

There is one error in the example that you listed.

I will fix that for you.

#!/bin/sh
# This is a comment!
echo "Hello World"; # This is a comment, too!

Once you complete entering the text you want you will need to save the file and you do this by doing the following:

you first hit the "esc" key then type in the following :wq! that will write the file and then quit just like going to file then hitting save and quit.

I have bolded the important things.

Of course this is all done via the terminal session. Depending on how you have your unix setup this maybe irrelevant.

Edited by cryptodan, 18 June 2010 - 09:24 AM.


#9 Reddoug

Reddoug
  • Topic Starter

  • Members
  • 113 posts
  • OFFLINE
  •  
  • Local time:06:08 PM

Posted 18 June 2010 - 07:41 PM

Thanks, I will check out the vi editor.

DOug




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users