LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-05-2009, 12:06 AM   #1
DasRipper
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
Writing a script that changes to the home directory


Wow I am such a newb, but I won't get any better at this unless I dive in, so here is the question in full that I am having trouble with and I'm sure you can all get a good laugh at:

Write a shell script called nbin. First the script changes to the home directory. Then if there is a bin directory under the current directory, display the message bin has XX files". (Note: the XX will be substituted with the actual number of files in your bin directory)

So far I have done:
vi nbin
#!/bin/bash
PATH=/bin:$HOME/bin





Gosh I am really struggling with these basics.

Any help would be greatly appreciated.
 
Old 10-05-2009, 12:19 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by DasRipper View Post
Wow I am such a newb, but I won't get any better at this unless I dive in, so here is the question in full that I am having trouble with and I'm sure you can all get a good laugh at:

Write a shell script called nbin. First the script changes to the home directory. Then if there is a bin directory under the current directory, display the message bin has XX files". (Note: the XX will be substituted with the actual number of files in your bin directory)

So far I have done:
vi nbin
#!/bin/bash
PATH=/bin:$HOME/bin





Gosh I am really struggling with these basics.

Any help would be greatly appreciated.
Code:
vi nbin
No wonder you're having so much trouble. Try using a more efficient editor than "vi".

I won't write the script for you, but here is what it needs to do:

1. Don't reset your path -- that's the wrong approach. So erase the one working line you typed into in your script.

2. Use the "cd" command (plus an argument) to change to your home directory.

3. Test for the presence of a "bin" directory.

4. If there is such a directory, list the files within it, but capture the list and count the lines that were created by files.

5. Print out how many lines were caused by files (not directories), and that's your result.
 
Old 10-05-2009, 12:29 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Sounds like you'll need
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

They should get you started.

Welcome to LQ


There's nothing wrong with vi/vim; it's a very efficient editor.
It's also pretty much guaranteed to exist on any *nix system, not always the case for the others, especially GUI editors.
Best is of course subjective; whatever suits you.
 
Old 10-05-2009, 12:31 AM   #4
DasRipper
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for the heads up on the vi, it's what we are learning with in class.

I am taking this all online so I don't feel I am getting to ask the really stupid questions I normally would if it was in class, so thanks for letting me bring them here.

so perhaps:

cd nbin > home

is at least the right path?
 
Old 10-05-2009, 12:33 AM   #5
DasRipper
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
Sounds like you'll need
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

They should get you started.

Welcome to LQ


There's nothing wrong with vi/vim; it's a very efficient editor.
It's also pretty much guaranteed to exist on any *nix system, not always the case for the others, especially GUI editors.
Best is of course subjective; whatever suits you.
Thank you! I think I can already tell this is easier for me to read than my professor's writing style.
 
Old 10-05-2009, 12:35 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try reading those links first, then just mess about with the basic cmds

ls

cd

wc

Use the manual pages:

man ls
man cd
man wc

or see them here
http://linux.die.net/man/


Edit: ok we're cross-posting; I'll leave you to it for a while.
 
Old 10-05-2009, 03:07 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Easier to play around with the commands at a command line to see what they do and familiarise yourself with them. Once you have found a sequence of commands that work it's a small next step to create the nbin script containing them and test it. Good general technique to break a big problem into bite-size chunks.
 
Old 10-05-2009, 10:56 AM   #8
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by chrism01 View Post
Sounds like you'll need
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

They should get you started.

Welcome to LQ


There's nothing wrong with vi/vim; it's a very efficient editor.
I would have objected to this view using vi, but I couldn't remember how to get vi to switch between its exclusive and cryptic move-cursor, insert-text, delete-text and select-text modes, each of which requires the user to remember the appropriate multiple command keystrokes as well as exit one mode before entering another.

I remind users that "nano" is available on most Linux distributions, precisely because vi is the only alternative and is incredibly inefficient. Nano, in spite of being a CLI editor, lives in the 21st century -- it takes advantage of the fact that modern computers display on glass terminals, not paper (the original reason for vi's weird behavior).

If you are a historian or a masochist, definitely ... use vi. If you want to get something done in a finite time interval, use nano.
 
Old 10-05-2009, 11:29 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by lutusp View Post
If you are a historian or a masochist, definitely ... use vi. If you want to get something done in a finite time interval, use nano.
Oh, no! Editor wars!

For nano is well-suited to casual use whereas vi needs a lot of learning but is very functional after doing that learning. nano is a great alternative to vi when the /usr file system is not mounted!

One of the great things about editor wars is that few people are masters of more than one editor so can give a balanced appraisal of the relative merits, of strengths and weaknesses, of suitability for particular tasks.
 
Old 10-05-2009, 12:26 PM   #10
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Why doesn't everyone just use the editor of their choice?

vi/vim is a perfectly acceptable text editor, and I'm sure your favorite editor is equally acceptable.

I personally like (prefer) vi but you are entitled to your opinion
 
Old 10-05-2009, 01:35 PM   #11
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Rep: Reputation: 30
Quote:
Originally Posted by DasRipper View Post
Thanks for the heads up on the vi, it's what we are learning with in class.

I am taking this all online so I don't feel I am getting to ask the really stupid questions I normally would if it was in class, so thanks for letting me bring them here.

so perhaps:

cd nbin > home

is at least the right path?
you've probably learned vim, which is in some distributions a soft link from vi to vim.
using vi today (the old version), is a waste of time.


p.s,
I'm a heavy vim user.
 
Old 10-05-2009, 05:38 PM   #12
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by Disillusionist View Post
Why doesn't everyone just use the editor of their choice?

vi/vim is a perfectly acceptable text editor, and I'm sure your favorite editor is equally acceptable.

I personally like (prefer) vi but you are entitled to your opinion
Yes -- I agree that we all should know how to use an abacus before daring to express an opinion about its merits compared to a spreadsheet. And before offering an opinion about suicide, we each should ... can you guess what's coming next?

I wrote a rather famous program about 30 years ago called "Apple Writer". A friend who had only used vi tried it and, in spite of the fact that it was running on a computer with the processing power of a blender, almost wet his pants. He simply hadn't realized you could add text, remove text, copy and paste, and move the editing cursor around without explicitly changing control modes for each and every one of these actions.

Everyone is entitled to his personal tastes. But newbies should not be steered toward vi without first being informed that it is a historical artifact of a bygone era, specifically designed to avoid wasting paper while being used on a paper-roll teletype machine.

One the other hand, some ingenious mechanics have recently recreated Babbage's Difference Engine, never fully realized in Babbage's lifetime. So I guess there's a place for vi.
 
Old 10-06-2009, 12:42 AM   #13
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
I would agree that newbie's should not be forced to learn vi as the only option for text editing. After all it is not the most intuitive text editor.

The teacher should introduce nano, kate or gedit as these would be much easier to get to grips with.

After all the intention is to write a script, it shouldn't matter what editor is used so the student should be free to use what they would feel most comfortable with.

As for suicide... I have already decided that it's not for me (I know, I never tried it or anything!)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Permissions for php script writing to directory noirboy Linux - Newbie 1 06-20-2009 09:48 PM
Help with bash script to create directory to /home folder wegadnie Linux - General 5 10-25-2008 11:50 AM
Writing a script to backup up /home/username ginda Programming 3 01-05-2006 06:47 AM
Home Directory Script dumbsheep Linux - Networking 2 10-28-2005 09:50 PM
writing to home directory from windows jsmarshall85 Linux - Networking 1 07-23-2004 09:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration