Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Tried finding an answer on stackoverflow but can't. All answers were with awk and sed which I'm not allowed to use! So once again, I ask you to help me.

Basically I need to write a script that prints out the history of all used commands in the linux terminal. The commands are saved in a file which is put as the first argument of the script. Command "/bin/bash" equals bash so we don't mix things up. So, the print out should be sorted from commands with most occurrences to commands with least occurrences. Those with the same number of occ should be sorted in alphabetical order.

And, if we put switch -n the script only prints out the first n commands. And with switch -p we also include the commands that are part of the pipeline.

Example of history.txt;

ls -al
bash
cat datoteka.txt
cat d.txt | sort | less
/bin/bash

What the script should print out;

./hstat.sh history.txt

2 bash
2 cat
1 ls

As I'm fairly new to BASH programming and not really good at it, I would love some help with this. Where to start, some useful examples, maybe some code or even a lot of it :P Of course, with explanation or else it would be useless for me to just copy it.
Posted
Updated 28-Apr-15 5:57am
v2

1 solution

It's a while since I did much bash scripting but you probably need to start by passing the file through sort, to get the commands in sorted order. Pass the sort output to uniq which, I think, has an option to count the lines. Check the man pages of each command for the applicable options.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900