Purchase Linux CDs / DVDs / Flash Drives at OSDisc.com

Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd, 2017 please sign up again. Thanks!

  1. More ways to get the info! - we shoot all of our new original content out as well as random messages on Twitter and our newsletter!. Twitter | Newsletter
    Dismiss Notice

Command line shell script doubt

Discussion in 'Command Line' started by gustagol, May 15, 2019 at 3:06 AM.

  1. gustagol

    gustagol New Member

    Joined:
    Wednesday
    Messages:
    2
    Likes Received:
    0
    I did not succeed in doing a shell script question, can anyone get a resolution? Or you can explain, I used grep cut awk and I could not do it anyway.
    It has been made available a txt file, I will post it summarized:




    (Log in to hide this advertisement)


    rtkit 1207 0.0 183544 tty / 7 14:11 00:00 / usr / lib / rtkit / rtkit-daemon
    root 1219 0.0 351272 tty / 7 14:11 00:00 / usr / lib / upower / upowerd
    root 1263 0.0 0 tty / 7 14:11 00:00 krfcommd
    user001 1276 0.0 45280 tty / 7 14:11 00:00 / lib / systemd / systemd
    user001 1279 0.0 63240 tty / 7 14:11 00:00 (sd-pam)
    user001 1284 0.0 209248 tty / 7 14:11 00:00 / usr / bin / gnome-keyring-daemon
    user007 1286 0.0 50640 tty / 7 14:11 00:00 / sbin / upstart
    root 1380 0.0 16128 tty / 7 14:11 00:00 / sbin / dhclient
    nobody 1391 0.0 56900 tty / 7 14:11 00:00 / usr / sbin / dnsmasq
    user001 1482 0.0 36896 tty / 7 14:11 00:00 upstart-udev-bridge
    user001 1547 0.0 43924 tty / 7 14:11 00:00 dbus-daemon

    For the following exercises, use the provided "data2019.txt" file. This file represents the programs running on a Linux system, containing, in each line, the following data:
    User, Program ID,% CPU Used, Total Memory Used, Terminal, Start Time, Run Time, Program
    In the file, each die is separated from the other by a space.


    Question:
    Write a shellscript that lists exactly how many subdirectories a user has in any directory received by parameter and report that quantity.
     
  2. dos2unix

    dos2unix New Member

    Joined:
    May 3, 2019
    Messages:
    24
    Likes Received:
    23
    There is a command called "tree" that does this for already.

    A quick one line way to do this would be ...

    ls -R * | wc -l

    But that has nothing to do with cut or awk.

    cut is how you separate "columns" of text.

    Using your example file above, you could do some exercises like...

    cat data2019.txt | cut -f1 -d:

    This will show you everything before the first ":"

    cat data2019.txt | cut -f2 -d:

    This will show you everything between the two ":"'s.

    cat data2019.txt | cut -f1 -d/

    Will show you everything before the first "/"

    In cut... the f flag in the field number. The d flag is the delimiter.

    You can also use character positions sometimes.

    cat data2019.txt | cut -b1-9

    You can also view multiple columns with cut.

    cat data2019.txt | cut -f2-3 -d:

    After you use it a few times, it gets easier :)
     
    Rob likes this.
  3. gustagol

    gustagol New Member

    Joined:
    Wednesday
    Messages:
    2
    Likes Received:
    0
    I did not understand how I'm going to pass a directory from one user per parameter to find the amount of subdirectories just with ls -R * | wc-1
     
  4. wizardfromoz

    wizardfromoz Super Moderator
    Staff Member Gold Supporter

    Joined:
    Apr 30, 2017
    Messages:
    2,980
    Likes Received:
    2,341
    G'day @gustagol and welcome to linux.org :)

    You have mis-typed the command @dos2unix provided.

    That is wc-l not wc-1 ... (l for lowercase L)... always copy and paste commands.

    That being said, we are not in the business of doing your homework or assignments for you, haven't the time.

    Have you asked your teacher/lecturer for some references to scripting?

    Otherwise we may be able to provide some links.

    Cheers

    Chris Turner
    wizardfromoz
     
    Vrai likes this.

Share This Page