Results 1 to 25 of 25

Thread: Anyone know UNIX shell scripting?

  1. #1
    Senior Member | IA Veteran man's Avatar
    Join Date
    Mar 2005
    Age
    37
    Posts
    6,690
    Rep Power
    28

    Default Anyone know UNIX shell scripting?

    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?
    IA Rules doesn't allow these images in sigs

    - IA Mgmt

  2. #2
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    Shell scripting is gravy, what shell are you using.. bash ?

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

  3. #3
    Senior Member | IA Veteran man's Avatar
    Join Date
    Mar 2005
    Age
    37
    Posts
    6,690
    Rep Power
    28

    Default

    Yeah, if that's what Ubuntu shell is. I don't know jack shit about Linux.
    IA Rules doesn't allow these images in sigs

    - IA Mgmt

  4. #4
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    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.

  5. #5
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    then you could also save the ld shell script into /bin and it would be global.

  6. #6
    Senior Member | IA Veteran man's Avatar
    Join Date
    Mar 2005
    Age
    37
    Posts
    6,690
    Rep Power
    28

    Default

    Do I just write it to a text file and save as .sh?
    IA Rules doesn't allow these images in sigs

    - IA Mgmt

  7. #7
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    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.

  8. #8
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    Have you ever worked with .bat files on windows/dos ?

  9. #9
    Senior Member | IA Veteran man's Avatar
    Join Date
    Mar 2005
    Age
    37
    Posts
    6,690
    Rep Power
    28

    Default

    alright so how do I specify the directory to open and it won't let me save it to bin
    IA Rules doesn't allow these images in sigs

    - IA Mgmt

  10. #10
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    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)

  11. #11
    Senior Member | IA Veteran man's Avatar
    Join Date
    Mar 2005
    Age
    37
    Posts
    6,690
    Rep Power
    28

    Default

    and I use VI in class but ubuntu's vi is a BITCH
    IA Rules doesn't allow these images in sigs

    - IA Mgmt

  12. #12
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    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.

  13. #13
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    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.

  14. #14
    Senior Member | IA Veteran man's Avatar
    Join Date
    Mar 2005
    Age
    37
    Posts
    6,690
    Rep Power
    28

    Default

    Quote Originally Posted by green91
    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
    IA Rules doesn't allow these images in sigs

    - IA Mgmt

  15. #15
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    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.

  16. #16
    Senior Member | IA Veteran man's Avatar
    Join Date
    Mar 2005
    Age
    37
    Posts
    6,690
    Rep Power
    28

    Default

    wow, it works, you are the man. Please bath yourself in my reps, for it is all I have to give
    IA Rules doesn't allow these images in sigs

    - IA Mgmt

  17. #17
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    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.

  18. #18
    Senior Member | IA Veteran man's Avatar
    Join Date
    Mar 2005
    Age
    37
    Posts
    6,690
    Rep Power
    28

    Default

    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
    IA Rules doesn't allow these images in sigs

    - IA Mgmt

  19. #19
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    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

  20. #20
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    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.

  21. #21
    Senior Member | IA Veteran man's Avatar
    Join Date
    Mar 2005
    Age
    37
    Posts
    6,690
    Rep Power
    28

    Default

    Well we just finished c, which I still don't understand. C++ is easier though
    IA Rules doesn't allow these images in sigs

    - IA Mgmt

  22. #22
    Mountain man green91's Avatar
    Join Date
    May 2003
    Location
    Dahlonega, Ga
    Posts
    8,975
    Rep Power
    45

    Default

    LOL well thats cool. Hit me up with any more questions.

  23. #23
    Senior Member | IA Veteran man's Avatar
    Join Date
    Mar 2005
    Age
    37
    Posts
    6,690
    Rep Power
    28

    Default

    aight man, thanks again
    IA Rules doesn't allow these images in sigs

    - IA Mgmt

  24. #24
    ASC is for fools Blitanicle99's Avatar
    Join Date
    Feb 2006
    Location
    Woodstock
    Posts
    4,028
    Rep Power
    24

    Default

    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.
    Honda RC51 SP1
    Yoshi RS-3 Cans
    520 Conversion
    Clip Ons
    Race Tech Fork Kit

  25. #25
    Senior Member | IA Veteran man's Avatar
    Join Date
    Mar 2005
    Age
    37
    Posts
    6,690
    Rep Power
    28

    Default

    Quote Originally Posted by Blitanicle99
    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.
    IA Rules doesn't allow these images in sigs

    - IA Mgmt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
ImportAtlanta is a community of gearheads and car enthusiasts. It does not matter what kind of car or bike you drive, IA is an open community for any gearhead. Whether you're looking for advice on a performance build or posting your wheels for sale, you're welcome here!
Announcement
Welcome back to ImportAtlanta. We are currently undergoing many changes, so please report any issues you encounter with the site using the 'Contact Us' button below. Thank you!