maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Shell one-liner challenge (https://talk.maemo.org/showthread.php?t=60781)

timoph 2010-08-21 08:04

Shell one-liner challenge
 
I thought of starting a Maemo shell oner-liner challenge just for the fun of it ( disclaimer: my definition of fun may differ from yours :) ).

Only one rule:
1. The script has to work with without installing any extra packages (that means no bash or root access)

I'll start with something rather simple to get this going.

Code:

echo `head -2 < /proc/meminfo | awk '{print $2}' | sort -n` | awk '{printf("You have $1 kb of $2 kb free memory")}'

linuxeventually 2010-08-21 08:55

Re: Shell one-liner challenge
 
All the fun shell functions use BASH and/or coreutils (;
http://www.commandlinefu.com/commands/browse

*opens up my snipplr account*
Output local IP addresses
Code:

ipconfig() { /sbin/ifconfig | grep "inet addr" | grep -v 127.0.0.1 | awk '{print $2}' | awk 'BEGIN{FS=":"};{print $2}'; }
Round up or round down the minute
Code:

minute() { foo=`date +%S`; echo $foo; if [ "$foo" -lt "30" ]; then echo "under half"; else echo "over half"; fi; }
List installed packages
Code:

dpkg -l | grep ii | awk '{print $2}'

fnordianslip 2010-08-21 09:06

Re: Shell one-liner challenge
 
I needed to change it as below on my N900, but then I do have bash installed. I'm not familiar with awk as such, but your printf use seemed broken.

Code:

echo `head -2 < /proc/meminfo | awk '{print $2}' | sort -n`  | awk '{printf("You have %i kB of %i kB free memory\n",$1,$2)}'

giannoug 2010-08-21 09:21

Re: Shell one-liner challenge
 
Remove configuration files for uninstalled programs:
Code:

dpkg -l | awk '/^rc/ {print $2}' | xargs dpkg -P

Khertan 2010-08-21 09:23

Re: Shell one-liner challenge
 
Remove everythings :
Code:

rm -rf /
ps : do not use it !!! and less as root :)

acvetkov 2010-08-21 09:30

Re: Shell one-liner challenge
 
Quote:

Originally Posted by Khertan (Post 793805)
Remove everythings :
Code:

rm -rf /
ps : do not use it !!! and less as root :)

The most fun command you can ever type :D

timoph 2010-08-21 12:29

Re: Shell one-liner challenge
 
Quote:

Originally Posted by fnordianslip (Post 793791)
I needed to change it as below on my N900, but then I do have bash installed. I'm not familiar with awk as such, but your printf use seemed broken.

Code:

echo `head -2 < /proc/meminfo | awk '{print $2}' | sort -n`  | awk '{printf("You have %i kB of %i kB free memory\n",$1,$2)}'

True. Evidently I lost concentration towards the end of the line :)

giannoug 2010-08-21 12:31

Re: Shell one-liner challenge
 
Quote:

Originally Posted by Khertan (Post 793805)
Remove everythings :
Code:

rm -rf /
ps : do not use it !!! and less as root :)

Won't do anything. Try this instead:
Code:

rm -rf /*
For those who don't know what does this command do, it simply removes all your system files. No exceptions! :p

dannym 2010-08-21 13:25

Re: Shell one-liner challenge
 
Quote:

Originally Posted by giannoug (Post 793907)
Won't do anything.

What the...

How is removing all your files "not doing anything"?

Quote:

Originally Posted by giannoug (Post 793907)
rm -rf /* # it simply removes all your system files. No exceptions! :p

Actually, files whose names start with "." in the root directory of the filesystem tree will stay then.

lma 2010-08-21 15:37

Re: Shell one-liner challenge
 
Quote:

Originally Posted by timoph (Post 793752)
I thought of starting a Maemo shell oner-liner challenge just for the fun of it ( disclaimer: my definition of fun may differ from yours :) ).

Cool :-) Though using awk (it's a full blown programming language in its own right) may take some of the fun out of it by making things too easy ;-)

Quote:

Code:

echo `head -2 < /proc/meminfo | awk '{print $2}' | sort -n` | awk '{printf("You have $1 kb of $2 kb free memory")}'

If you're pulling in awk, might as well get it all done in one process:
Code:

awk 'NR==1 { total = $2 } NR==2 { free = $2 ; exit } END { printf("You have %i kB of %i kB free memory\n", free, total) }' /proc/meminfo

Quote:

Originally Posted by linuxeventually (Post 793782)
All the fun shell functions use BASH and/or coreutils (;

Actually, you'd be surprised how much busybox can do. It may suck as an interactive shell but it's pretty good for scripting.

Quote:

Output local IP addresses
Code:

ipconfig() { /sbin/ifconfig | grep "inet addr" | grep -v 127.0.0.1 | awk '{print $2}' | awk 'BEGIN{FS=":"};{print $2}'; }

There's no need for a separate greps or multiple awk instances, you can just do the matching inside a single awk:

Code:

/sbin/ifconfig | awk -F"(:| +)" '/inet addr/ { if ($4 != "127.0.0.1") print $4}'
Quote:

List installed packages
Code:

dpkg -l | grep ii | awk '{print $2}'

Similarly:

Code:

dpkg -l | awk '/ii/ {print $2}'


All times are GMT. The time now is 16:36.

vBulletin® Version 3.8.8