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.
I need a tutor for linux. I have spent countless hours struggling with writing very basic script shells and have come to realize this isnt what i am going be doing for a living. I cant seem to get the help I need from my teacher, book or the internet. I am a cisco major who had to take an intro to linux class. That has found out it is basically a programming class. I have no interest in writing script shells however I still need to pass the class. Im not looking for anyone to do my homework, but i do need some serious one on one help that i am willing to pay for. I live in southeast michigan and am willing to drive up to an hour. Thanks in advance
Scripting isn't really that hard so long as you remember that any series of commands you type at command line can be a script. For example if you wanted to see how much space was in your current filesystem you'd type "df -h .". To see what the permissions/ownership of the current directory are you'd type "ls -d ." To create a script that showed you both you can just edit a file and put those two commands in it:
Code:
df -h .
ls -d .
You then have to make the file executable (e.g. chmod 755 <filename>). Once you do that you can run the script by typing "./<filename>". You can run it in other directories by putting in full path (e.g. /home/mydir/<filename>). You can do it without typing in full path by adding the directory it is in to your PATH variable (e.g. PATH=$PATH:/home/mydr) then you can run <filename> without having to specify ./ or full path.
It really is that easy to make "a" shell script. There is generally more you should but don't HAVE to do. One example is that most scripts have an interpreter line that tells it what shell to use so the above script would be:
Code:
#!/bin/bash
df -h .
ls -d .
That tells it to use /bin/bash to run the commands that follow. (bash is the default shell on most systems but there are others and by adding this first line you insure that the script is always run as bash even if the users started in something else like dash, ksh or csh.)
Other things might be to do error checking and setting variables. The tutorial link (or any other you find) should go over this.
You may end up needing to know more about command line/scripting than you think. CISCO has its own shell scripting.
GUIs are nice but real admins use CLI and scripting.
Maybe my scripts are harder than I thought because i get the commands I understand the permissions. The problem im having is how it all fits together. for instance, a part of my assignment was to write a program that shows and calculates a multiplication table 1-10 using a nested loop and use different options of the echo command to display the results. I found some examples that kind of helped me write the program however it wasnt a nested loop. I could not find anything like that. I have only been working with any kind of cli for 5 weeks, and some of these assignments do not seem very "intro" to me.
'
'
#!/bin/bash
for x in 1 2 3
do echo Doing subroutine for $x
for y in 4 5 6
do echo Multiplying $x times $y
expr $x \* $y
done
done
What makes this "nested" is that you're doing one loop inside another. The first loop is acting on 1 then on 2 then on 3. When it acts on 1 it is multiplying 1 times 4 then times 5 then times 6. It then does the same thing by multiplying 2 times 4 then times 5 then times 6 and finally does it for 3.
You might want to set your expr command as a variable to be used in multiple echo statements required by your assignment so you aren't having to redo the calculation each time. Doing that is simple:
Code:
#!/bin/bash
for x in 1 2 3
do echo Doing subroutine for $x
for y in 4 5 6
do echo Multiplying $x times $y
RESULT=$(expr $x \* $y)
echo Result is $RESULT
done
done
Note that variables (other than positional parameters and other special ones) are entirely arbitrary. The above script could be written as:
Code:
#!/bin/bash
for billybob in 1 2 3
do echo Doing subroutine for $billybob
for suzyq in 4 5 6
do echo Multiplying $billybob times $suzyq
RalphieMay=$(expr $billybob \* $suzyq)
echo Result is $RalphieMay
done
done
Your various echo options can be determine by typing "man echo". (Note that bash has a built in echo command which is separate from the echo command the man page discusses - much of it is the same but if something doesn't work the way you'd expect you need to type "man bash" and go to the discussion it has for its internal echo to determine differences.) A common echo option is to put "\n" at the end of a what you're echoing to add a new line. There are other characters for tabs etc...
You can also find out what other commands do by using man page. (For example "man expr" will show you what the expr command is used for.)
P.S. Although I indented to make it clear where the nested loop starts and ends within the main loop this isn't required - it does however make it easier to read for a couple of levels of nesting - for more it might get unwieldy and you'd probably want to look at functions but hopefully that is something later in your class.
Last edited by MensaWater; 08-08-2011 at 02:27 PM.
Thank you for those examples. I believe with those examples I can probably muddle through it. Its funny that you mention functions because they are the next part of the assignment. this part will probably keep me busy the rest of the day. I hate begging you all for help, but at this point I dont know where else to turn. Thanks again
If you're looking for local people, you should look for a Linux User Group. Here is a list of some in Michigan. There may be others, so do some googling.
Also, you don't use apostrophes in plurals, so "tutor's" should be "tutors".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.