Command line shell script doubt

gustagol

New Member
Joined
May 15, 2019
Messages
2
Reaction score
0
Credits
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:



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.
 


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 :)
 
  • Like
Reactions: Rob
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 :)
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
 
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
 

Members online


Top