PDA

View Full Version : Anyone know UNIX shell scripting?



man
02-18-2009, 07:57 PM
So, I'm sitting here trying to start an assignment and have no idea where to start.

Implement a UNIX shell program (ld) that will list only directories in the directories on the command line. A command with no arguments assumes the current directory.

Anyone know how to do this?

green91
02-18-2009, 08:00 PM
Shell scripting is gravy, what shell are you using.. bash ?

*turns on linux server to get the correct command line argument*

man
02-18-2009, 08:03 PM
Yeah, if that's what Ubuntu shell is. I don't know jack shit about Linux.

green91
02-18-2009, 08:08 PM
Yes ubuntu uses bash by default.

Seems like the simplest way to do this would be



#!/bin/bash
ls-D


then save it as ld
chmod 755 ld
should be good to go.

green91
02-18-2009, 08:09 PM
then you could also save the ld shell script into /bin and it would be global.

man
02-18-2009, 08:09 PM
Do I just write it to a text file and save as .sh?

green91
02-18-2009, 08:10 PM
Yep that would work. But youve gotta make sure to chmod 755 to make it executable. You should really practice command line file editing.. most purists use Vi but im very partial to pico (its easier for quick edits since there isnt a ton of key mapping memorizations.)

edit: you could more easily chmod +x the script also to preserve correct owner permissions.

green91
02-18-2009, 08:16 PM
Have you ever worked with .bat files on windows/dos ?

man
02-18-2009, 08:16 PM
alright so how do I specify the directory to open and it won't let me save it to bin

green91
02-18-2009, 08:19 PM
Ah we need to pass an argument then, okay try this instead:



#/bin/bash
ls -D $1


$1 is the first passed argument string.

So then to execute the shell script you would do like:

./ld.sh /root

which would list hte directory contents of /root etc.

you wont be able to save to /bin unless you do it as root (sudo on ubuntu)

man
02-18-2009, 08:20 PM
and I use VI in class but ubuntu's vi is a BITCH

green91
02-18-2009, 08:20 PM
Ignore than i even mentioned putting it in /bin, im sure your instructor doesnt intend for you to do that. you would also need to remove .sh from the file name.

green91
02-18-2009, 08:21 PM
Yea VI has its advantages in the programming world but its a little bit aggravating to learn LOL. Ubuntu uses the same VI as every other posix compliant o/s.

man
02-18-2009, 08:23 PM
Yea VI has its advantages in the programming world but its a little bit aggravating to learn LOL. Ubuntu uses the same VI as every other posix compliant o/s.

Well for some reason I can't use the arrow keys, cant use backspace etc.. in my VI

Also I'm getting error: ./ld.sh: line 2: ls-D: command not found

green91
02-18-2009, 08:24 PM
there is a space between ls and -D.

LS is the command to list directory contents. -D is an argument to LS that only lists directories.

man
02-18-2009, 08:26 PM
wow, it works, you are the man. Please bath yourself in my reps, for it is all I have to give

green91
02-18-2009, 08:28 PM
LOL not a prob man.

Shell scripts are really easy once u kinda get the hang of how they work.
They are basically just an automated list of commands you would normally type at the prompt. Then shell scripts also allow for you to add additional options like passing arguments (like we did with $1 as the first string) and if/then type arguments etc. They work very similiar to batch files with dos.

man
02-18-2009, 08:29 PM
Gotcha, problem is my Prof doesn't teach us SHIT. We have to learn all of this by ourselves (that's what he told us today) plus c and c++.

I'm a java master but this stuff confuses the hell outta me

green91
02-18-2009, 08:31 PM
Oh yeah don't forget about man pages and --help arguments.

for instance, i wasnt sure how to only list directories within a directory so i typed:

ls --help

which helped me to find the -D argument to list only directories.

man pages are short for manual.. aka

man ls

or man vi

green91
02-18-2009, 08:32 PM
Well have fun with C, its heavy shit especially compared to shell scripting. its over my head lol. i enjoy *nix networking etc and shell scripting can help to automate things quite a bit.

man
02-18-2009, 08:33 PM
Well we just finished c, which I still don't understand. C++ is easier though

green91
02-18-2009, 08:34 PM
LOL well thats cool. Hit me up with any more questions.

man
02-18-2009, 08:34 PM
aight man, thanks again

Blitanicle99
02-18-2009, 11:23 PM
what class is this, I wanna take it.

I am a year into linux, like a month into basic script. I would love to learn more.

man
02-19-2009, 12:54 AM
what class is this, I wanna take it.

I am a year into linux, like a month into basic script. I would love to learn more.

It's not really a Linux class, just System Software. My professor just happens to like linux.