Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Console / shell tips & tricks
View unanswered posts
View posts from last 24 hours

Goto page Previous  1, 2, 3 ... 6, 7, 8 ... 14, 15, 16  Next  
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Thu Mar 04, 2004 12:37 pm    Post subject: Reply with quote

carambola5 wrote:
I use the alias lsd differently:
Code:
alias lsd='ls -d'

Very useful for using ls with a wildcard. For example, try:
Code:
ls -d /etc/g*

and
Code:
ls /etc/g*

You get very different results, and I'm usually looking for the first one.


I use this too, it comes in very handy when you're looking for something in the portage tree but cant remember what category it's in, it's quicker than any search program:

Code:
lsd /usr/portage/*/package_name

_________________
Search | Read | Answer | Report | Strip
Back to top
View user's profile Send private message
simulacrum
Tux's lil' helper
Tux's lil' helper


Joined: 30 Nov 2002
Posts: 128
Location: St Paul, MN

PostPosted: Fri Mar 12, 2004 7:57 pm    Post subject: Reply with quote

Can anyone explain this statement?

Quote:
:(){ :|:&};:


I'm not terribly familiar with bash programming and can't make heads or tails of it. From what I understand it forks bash shells until you run out of memory, but I'd like it broken down to understand the syntax. I've searched around and haven't found much that explains this statement. Thanks!
Back to top
View user's profile Send private message
Andersson
Guru
Guru


Joined: 12 Jul 2003
Posts: 525
Location: Göteborg, Sweden

PostPosted: Fri Mar 12, 2004 8:14 pm    Post subject: Reply with quote

simulacrum wrote:
Can anyone explain this statement?
Quote:
:(){ :|:&};:

I'm not an expert, but I recognize :(){} as a function definition. So every time you type a colon, the instructions inside the curly braces will be executed. After the function definition is a semicolon to end the first statement, and a colon to launch the function and start the whole chain.

So what's inside the function? :|:& -it starts itself and pipes the output to another instance of itself, everything in the background. So I suppose every function call results in two instances of the function, each of those in two new instances and so on.

I'm not sure about the last part.
Back to top
View user's profile Send private message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Fri Mar 12, 2004 8:28 pm    Post subject: Reply with quote

Andersson wrote:
I'm not sure about the last part.


The last semicolon lets you run more code afterwards on the same line and the final colon is the call to the function which gets the whole thing started.
_________________
Search | Read | Answer | Report | Strip
Back to top
View user's profile Send private message
SIR
n00b
n00b


Joined: 12 Jun 2003
Posts: 71
Location: DMCA and RIAA Land

PostPosted: Fri Mar 12, 2004 8:32 pm    Post subject: Reply with quote

Try some of the scripts found in this book.

Wicked Cool Shell Scripts
_________________
K6-2 333 160MB
gentoo-dev-sources 2.6.8-r3
Back to top
View user's profile Send private message
adammc
Apprentice
Apprentice


Joined: 07 Oct 2003
Posts: 230
Location: Europe

PostPosted: Fri Mar 12, 2004 8:35 pm    Post subject: Reply with quote

something I once used to generate a html page with thumbnails of a diretory of images:
Code:
echo "<html><head><title>Images</title></head><body>" > index.html
for f in `ls`
do
convert -size 200x400 $f -resize 200x400 thumb-$f
echo "<a href=\"$f\"><img src=\"thumb-$f\" /></a>" >> index.html
done
echo "</body></html>" >> index.html


just type it as-is into the bash prompt (including new lines) - requires imagemagick
_________________
There'd better be fudge when I get home...
Back to top
View user's profile Send private message
Andersson
Guru
Guru


Joined: 12 Jul 2003
Posts: 525
Location: Göteborg, Sweden

PostPosted: Fri Mar 12, 2004 8:49 pm    Post subject: Reply with quote

tomk wrote:
Andersson wrote:
I'm not sure about the last part.

The last semicolon lets you run more code afterwards on the same line and the final colon is the call to the function which gets the whole thing started.

I meant I wasn't sure about the last part I wrote, the : | : & part. Feel free to correct me on that one as well ;)

adammc wrote:
something I once used to generate a html page with thumbnails of a diretory of images
[...]

Nice. I like imagemagick, it's one of my favorite programs, all categories!
Back to top
View user's profile Send private message
Jefklak
l33t
l33t


Joined: 26 Oct 2003
Posts: 818
Location: Belgium

PostPosted: Fri Mar 12, 2004 8:58 pm    Post subject: Reply with quote

Code:
alias openwin="mount /dev/hdc1 && cd /mnt/windows/"
alias rm="rm -r"  # delete dirs without annoyance
alias ls="ls -l"   # standard listview
alias lss="ls"    # the 'old' way.
alias sys="/etc/init.d/"


Not impressive, but very handy.

Code:
syssamba restart


Is way shorter than typing in the full path :)
Back to top
View user's profile Send private message
benow
Tux's lil' helper
Tux's lil' helper


Joined: 02 Jun 2003
Posts: 84

PostPosted: Sat Mar 13, 2004 1:36 am    Post subject: xargs Reply with quote

xargs in combination with find allows for some funky voodoo:



Code:
find ~ -name \*.sh -type f | xargs --replace=+ cp + +.bak


The above creates a copy (with a .bak extension) of all scripts (*.sh) in the current users' home directory and below. The same trick is useful in conjunction with grep, rm, etc.
Back to top
View user's profile Send private message
Unne
l33t
l33t


Joined: 21 Jul 2003
Posts: 616

PostPosted: Sat Mar 13, 2004 2:42 am    Post subject: Reply with quote

When I'd be trying to tab-complete a file or directory in ~, it'd always prompt me "Show all 141 files?" or whatever, since there are so many hidden directories full of config data in users' home directories. And actually get to the list and you have a huge, hideous group of a hundred items displayed, 99% of which are never what you want. Put this in your /etc/inputrc

Code:
set match-hidden-files off


and bash will ignore hidden files when doing tab-completion. Great what you can find in man pages.
Back to top
View user's profile Send private message
neenee
Veteran
Veteran


Joined: 20 Jul 2003
Posts: 1786

PostPosted: Sat Mar 13, 2004 9:28 am    Post subject: Reply with quote

wow. thanks :D
Back to top
View user's profile Send private message
CrashPat
Tux's lil' helper
Tux's lil' helper


Joined: 01 Mar 2004
Posts: 97

PostPosted: Sat Mar 13, 2004 9:53 am    Post subject: Reply with quote

I had it ask, show 1841 files? today. I had to say no. :P

:roll:
Back to top
View user's profile Send private message
Donovan
Tux's lil' helper
Tux's lil' helper


Joined: 08 Feb 2003
Posts: 97
Location: Halifax, NS, Canada

PostPosted: Tue Mar 16, 2004 4:44 pm    Post subject: Reply with quote

Jefklak wrote:
Code:

alias sys="/etc/init.d/"


Not impressive, but very handy.

Code:
syssamba restart


Is way shorter than typing in the full path :)


Interesting, that doesn't work on my system... :?

Code:
#Use the mouse, smooth scrolling, and disable word wrap
alias nano="echo \>\> Using Nano options: -w --mouse --smooth && nano -w --mouse --smooth"

#Show Emerge results in column format
alias emerge="echo \>\> Using Emerge options: --columns && echo Starting Emerge... && emerge --columns"

#Show Emerge results in column format and use unstable tree
alias ~emerge='echo \>\> Using options: --columns \(and unstable packages\) && echo Starting Emerge...  && env ACCEPT_KEYWORDS=~x86 emerge --columns'


I like to be reminded that I'm using switches, on slower systems it's sometimes nice to know that emerge IS going to start eventually :), and the last option is pretty cool, beats typing 'env ACCEPT_KEYWORDS=~x86' to get the latest unstable version of a package.
_________________
Gentoo Linux * DirecTivo * Who needs heaven?
Back to top
View user's profile Send private message
aja
l33t
l33t


Joined: 26 Aug 2002
Posts: 705
Location: Edmonton, Canada

PostPosted: Tue Mar 16, 2004 5:56 pm    Post subject: Reply with quote

simulacrum wrote:
Can anyone explain this statement?
Quote:
:(){ :|:&};:



Andersson is correct: the nature of the shell is that it forks a new process to handle the backgrounded pipes. This means that the first call forks two new processes, each of which fork two new processes, etc.

The technical name for this technique is a 'fork bomb' - it is a quick way to get your account cancelled in a multi-user environment.

http://info.astrian.net/jargon/terms/f/fork_bomb.html
Back to top
View user's profile Send private message
regeya
Apprentice
Apprentice


Joined: 28 Jul 2002
Posts: 270
Location: Desoto, IL, USA

PostPosted: Tue Mar 16, 2004 6:58 pm    Post subject: Reply with quote

gurke wrote:
steveb wrote:
this one starts manny bash prcesses and you can watch the cpu going crazy:
Code:
:(){ :|:&};:


cheers

SteveB


btw: don't do it if you are not fast enought to do an killall bash!


just tried if i am fast enough. --- i was not.
seems that it didnt hurt my computer. evrythings working fine.
though i ll never do it again.


For giggles, I just tried it on a Panther (MacOS X 10.3) box. It tied up the CPU for all of 3 seconds. :D
Back to top
View user's profile Send private message
Jefklak
l33t
l33t


Joined: 26 Oct 2003
Posts: 818
Location: Belgium

PostPosted: Tue Mar 16, 2004 7:25 pm    Post subject: Reply with quote

Stupid bastards!
Lol. I was so dumb to try it. Linux flipped completely.
Had to power down manually :(
Back to top
View user's profile Send private message
adammc
Apprentice
Apprentice


Joined: 07 Oct 2003
Posts: 230
Location: Europe

PostPosted: Tue Mar 16, 2004 7:27 pm    Post subject: Reply with quote

this is where setting up /etc/limits would be a good idea :)
_________________
There'd better be fudge when I get home...
Back to top
View user's profile Send private message
gurke
Apprentice
Apprentice


Joined: 10 Jul 2003
Posts: 260

PostPosted: Tue Mar 16, 2004 9:26 pm    Post subject: Reply with quote

how about something simple. press ctrl-r for searching in your history.
Back to top
View user's profile Send private message
wilho
Apprentice
Apprentice


Joined: 22 Jul 2002
Posts: 169

PostPosted: Sat Apr 24, 2004 9:46 am    Post subject: Reply with quote

Here's my configuration,
stuff is mostly from this thread, some of them are modified, something I came up with, something from other threads. Hopefully useful for others too.

There's one problem with that screen though, it somehow disables mousewheel scrolling in xterm. Any ideas why?

Code:

#allways switch to screen, reattach deattached, exit when screen exits
if [ $TERM == "screen" ] ; then
    PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\033\\"'
elif [ $TERM != "linux" ] ; then
    screen -RR
    exit
fi

source /etc/profile
[ -f /etc/profile.d/bash-completion ] && source /etc/profile.d/bash-completion

PS1="\033[32m\]\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files, \$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')"'  \[\033[1;34m\]\w\n\[\033[0m\][\[\033[1;31m\]\u@\h\[\033[1;34m\] $(/usr/bin/tty | /bin/sed -e '\''s:/dev/::'\'')\[\033[0m\]] \[\033[0m\]'

alias cplay="curdir=\"`pwd`\" && cd /mnt/hdc2/My\ Music/ && cplay && cd \"$curdir\" && unset curdir"
alias grep="grep --color=auto"
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias rd=rmdir
alias md=mkdir
alias cls=clear
alias nano='nano -w'
alias inject='eject -t'
alias screen="screen -a"
alias fc-cache="fc-cache -fv"
alias updatedb="updatedb --prunepaths='/mnt /usr/src /usr/portage'"
alias rdate="echo Before: `date` && rdate -sup time.nist.gov"
alias updatecheck="nice emerge rsync && nice emerge -up world"
alias prelinkall="prelink -afmR"
alias systemupdate="rm /var/log/emerge.log && rdate && emerge rsync && emerge --upgradeonly --deep system && emerge --upgradeonly --deep world && rm -Rf /usr/portage/distfiles/  && prelinkall && updatedb"
alias bdu='echo -e "\033[01;33mSize,MB\tDIR\n------------------------\033[00m";du -msch --exclude={dev,proc,sys,mnt} `pwd`/* |sort -n'
alias lsd="ls -alhd */"
alias ls="ls --color"
function ll() { ls -alhF "$@" |more; }
function lr() { ls -alhtrF "$@" |more; }
function wgetresume {
        wget -ba $1 `head -n 1 $1 | sed s/^[0-9\ :-]*//g`
}
function recal() {
    if [ ! $@ ] ; then
       echo "Usage: recal PATTERN"
       echo "where PATTERN is a part of previously given command"
    else
        history | grep $@ |more;
    fi
}
function untar() {
    if [ ! "$1" ] || [ "$2" ] ; then
       echo "Usage: untar FILENAME"
       echo "where FILENAME is gzip, bzip2 or zip tar-archive"
    else
        TAR=$(which tar)
        UNZIP=$(which unzip)
        RAR=$(which rar)
        # returns gzip, bzip2 or Zip
        TYPE=$(file "$1" | cut -d " " -f 2)
        NOEXT=$(echo "$1" |cut -d "." -f 1)
        echo $TYPE

        if [ $TYPE == "gzip" ]; then
          $TAR -xzf "$1"
        elif [ $TYPE == "bzip2" ]; then
          $TAR -xjf "$1"
        elif [ $TYPE == "Zip" ]; then
          echo file is a .zip!
          unzip -d $NOEXT "$1"
        elif [ $TYPE == "RAR" ]; then
          echo file is a .rar!
          rar x "$1" $NOEXT
        fi

        unset TAR
        unset UNZIP
        unset RAR
        unset TYPE
        unset NOEXT
    fi
}
function update-love
{
    pushd /usr/local/portage/sys-kernel/love-sources
    rm *.ebuild
    for i in $(lynx -dump http://www.public.iastate.edu/~jpcox/ |awk '/ebuild/{print $2}'); do
        wget ${i}
        ebuild $(basename ${i}) digest
    done
    popd
}
function nX()
{
  for i in `seq 0 9`
  do
    if [ ! -e "/tmp/.X${i}-lock" ] ; then
      XFree86 ":$i" -query localhost &
      break
    fi
  done
}
# findfile -- a utility for finding files and directories
function findfile()
{
    case $1 in
        fn) find -iname $2 -print 2>/dev/null | more;;
        fs) find -size +$2k -print 2>/dev/null | more;;
        fm) find -cmin -$2 -print 2>/dev/null | more;;
        fd) find -iname $2 -type d -print 2>/dev/null | more;;
        fds) du -m * 2>/dev/null | sort -rn | head -$2;;
        ffs) PERCENTAGE=$2
          df -kl | grep -iv filesystem |  awk '{  print $6" "$5} '| while
              read LINE; do
               PERC=`echo $LINE | cut -d"%" -f1 | awk '{  print $2 } '`
               if [ $PERC -gt $PERCENTAGE ]
                   then
                 echo $LINE
               fi
                done;;
       fbad) find . -type l -print 2>/dev/null | perl -nle '-e || print';;
        *) echo "findfile usage:"
          echo
          echo "fn <name>   find file <name>, case insensitive, errors suppressed, wildcards OK"
          echo "fs <n>      find file greater than <n> kilobytes"
          echo "fm <n>      find file modified within last n minutes"
          echo "fd <name>   find directory <name>, case insensitive"
          echo "fds <n>     find top <n> directories, based on diskspace usage (in megabytes)"
          echo "ffs <n>     find file systems with usage greater than <n> percent"
          echo "fbad        find bad symlinks"
          echo
        esac
      unset PERCENTAGE
      unset PERC
}
#Shell options:
shopt -s cdspell #error correction
shopt -s cdable_vars
shopt -s checkhash
shopt -s checkwinsize
shopt -s mailwarn
shopt -s sourcepath
shopt -s no_empty_cmd_completion
shopt -s histappend histreedit
shopt -s extglob

echo "Welcome" `grep "^$USER" /etc/passwd|cut -f 5 -d:|cut -f1 -d,`
echo "`date "+%A %d.%m.%Y %H:%M, %Z %z"`"

edit: some fixes


Last edited by wilho on Sat Apr 24, 2004 9:53 pm; edited 3 times in total
Back to top
View user's profile Send private message
meowsqueak
Veteran
Veteran


Joined: 26 Aug 2003
Posts: 1549
Location: New Zealand

PostPosted: Sat Apr 24, 2004 11:54 am    Post subject: Reply with quote

Screen uses it's own scrollback buffer, rather than your xterm's. Unfortunately, screen has no support for the mouse wheel that I'm aware of (it uses a special 'copy' mode for scrollback) and I'm not sure even if it did that the xterm would pass the scroll events to the console app. If you can get this working without the usual ^a [ combo, please let me know!
Back to top
View user's profile Send private message
vdboor
Guru
Guru


Joined: 03 Dec 2003
Posts: 592
Location: The Netherlands

PostPosted: Sun Apr 25, 2004 2:06 pm    Post subject: Reply with quote

[quote="wilho"]
Code:

#Shell options:
shopt -s cdspell #error correction
shopt -s cdable_vars
shopt -s checkhash
shopt -s checkwinsize
shopt -s mailwarn
shopt -s sourcepath
shopt -s no_empty_cmd_completion
shopt -s histappend histreedit
shopt -s extglob



What do all these options do? "man bash" didn't always give me a clear answer.
_________________
The best way to accelerate a windows server is by 9.81M/S²
Linux user #311670 and Yet Another Perl Programmer

[ screenies | Coding on KMess ]
Back to top
View user's profile Send private message
wilho
Apprentice
Apprentice


Joined: 22 Jul 2002
Posts: 169

PostPosted: Sun Apr 25, 2004 5:25 pm    Post subject: Reply with quote

http://www.gb.nrao.edu/~jford/bash/bashref.html#SEC63
Back to top
View user's profile Send private message
Fred Bradstadt
n00b
n00b


Joined: 09 Jun 2003
Posts: 21
Location: Copenhagen, Denmark

PostPosted: Sat May 08, 2004 1:40 pm    Post subject: Re: FORTUNE RULES!!!! Reply with quote

Redson wrote:
That is awesome! You inspired me to write my first pseudo bash script!!!!
It essentially picks a certain type of fortune depending on what day it is.
I put it in my bashrc:

Code:
NUMBER_OF_FORTUNES=3
DATE=`eval date +%d`  #Produces only the date Number.

let "DATE=$DATE%$NUMBER_OF_FORTUNES"

case "$DATE" in
   [0]  ) fortune;; #Random fortune
   [1]  ) fortune /usr/share/fortune/starwars;;  #Quoute from Star Wars
   [2]  ) fortune /usr/share/fortune/homer;;  #Quote from Homer
esac


It took me a while to figure out why this script doesn't work on the 8th and 9th day of every month. But reading man date revealed to me that date %d returns "day of month (01..31)"; and man bash revealed that "Constants with a leading 0 are interpreted as octal numbers"2...
So to get the script to work correctly, exchange line 2 with
Code:
DATE=`eval date +%e`  #Produces only the date Number.
Back to top
View user's profile Send private message
hypercard
n00b
n00b


Joined: 22 Apr 2004
Posts: 13

PostPosted: Tue May 25, 2004 5:53 am    Post subject: Thanks Reply with quote

Thanks a ton Andersson! That remind trick really helps me out. I used to keep a notes file that I would open but now it's always waiting for me on the command line. It's great for little tips you find as you browse the forums.
Back to top
View user's profile Send private message
tam1138
Tux's lil' helper
Tux's lil' helper


Joined: 28 Oct 2003
Posts: 103

PostPosted: Wed May 26, 2004 10:40 pm    Post subject: Reply with quote

I use the carets quite a bit:
Code:
# emerge -pv system
# ^-pv^

or:
Code:
$ ls foo
$ ^foo^bar

Also, using the ":p" option on any potentially-dangerous bash replacement tricks is useful: it prints out the command that WOULD be run and also appends it to your history. That is, if you think you want to run the last command starting with 'r' in your history but don't want to hose yourself before verifying the command:
Code:
$ !r:p
rm -rf /

This is fun, too. To return to the previous directory (ie, $OLDCWD):
Code:
$ cd -
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Goto page Previous  1, 2, 3 ... 6, 7, 8 ... 14, 15, 16  Next
Page 7 of 16

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum