Writing a script that changes to the home directory
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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!)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.