16 мар. 2013 г.

Bash: Let me bash that for you, Part 2

Is your buffalo wings so spicy ?

Lets juzz it up a bit more with bash !

other parts of «let me bash that for you» Part 0 and Part I

  • var=
    creates a variable and assigns some value (e.g result command execution) to it for its future usages.
    Note: use a dollar sign ($) to reference a variable's value
    $ foo="FOO VARIABLE" ; echo "$foo $foo"
    FOO VARIABLE FOO VARIABLE
  • export variable=value
    export the variable value for the future usage
    $ export A=5
    $ echo $A
    5
  • set
    prints out all known (assigned) variables (embedded functions)

    Note: The list of the most commonly used and significant variables
    HOME home directory of the user, usually /home/username
    PATH paths to look for executable commands
    LD_LIBRARY_PATH - paths to look for dynamic shared libraries an application has been linked against
    PWD (actual) working directory
    OLDPWD old working directory
    JAVA_HOME usually sets as java (jdk) directory
    
    USER current user login name
    UID current user's user identification number, as recorded in /etc/passwd
    HOSTNAME the name of the current host
    
    
    TMOUT if set to a value greater than zero, bash terminates (log out) after that number of seconds if input does not arrive
    
    
    $? exit value of last executed command.
    $! pid of last background command.
    
    
    $0, $1, $2, etc - positional parameters, passed from command line to script, passed to a function
    $*, $@ all parameters passed from command line to script ($1 $2 etc)
    $# number of command-line arguments or positional parameters
    
    
    $$ pid of the current instance of shell
    PPID pid of parent process ( of the current instance of shell )
    
  • unset variable
    discards the variable
  • `command` and $(command)
  • variable assignment mechanism though a result of command execution
    $ NOW=`date +"%m-%d-%Y"`; echo $NOW
    03-16-2013
    $ NOW2=$(date +"%m-%d-%Y"); echo $NOW2
    03-16-2013
    
  • df
    reports disk space capacity/usage
    $ df
    Filesystem     1K-blocks     Used Available Use% Mounted on
    rootfs          24776880 19823040   3715004  85% /
    /dev/root       24776880 19823040   3715004  85% /
    devtmpfs         1551036        0   1551036   0% /dev
    tmpfs            1551228     2720   1548508   1% /run
    cgroup_root        10240        0     10240   0% /sys/fs/cgroup
    tmpfs             540672     5156    535516   1% /dev/shm
    /dev/sda2       31814768 29868180    355852  99% /home
    
    Hint: specifying path get space usage of corresponding file system
    $ df ~/workspace/
    Filesystem     1K-blocks     Used Available Use% Mounted on
    /dev/sda2       31814768 29868180    355852  99% /home
    
    Hint: option -h prints in human readable format
    $ df -h ~/workspace/
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda2        31G   29G  348M  99% /home
    
    Hint: use . (dot) to reffer actual file system
    $ df -h .
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda2        31G   29G  348M  99% /home
    

  • du
    estimates file space usage
    $ du myfile
    4 myfile
    $ du -h ~/workspace/docs/cs/java/langspec-3.0.pdf 
    7.8M /home/user/workspace/docs/cs/java/langspec-3.0.pdf
    
    Hint: option -s displays summarized info
    $ du -hs ~/workspace/docs/cs/java/
    25M /home/user/workspace/docs/cs/java/
    
  • file determines a file type by its content
    $ file ~/workspace/docs/cs/java/langspec-3.0.pdf
    /home/bob/workspace/docs/cs/java/langspec-3.0.pdf: PDF document, version 1.3
    $ cp ~/workspace/docs/cs/java/langspec-3.0.pdf /tmp/xxx
    $ file /tmp/xxx
    /tmp/xxx: PDF document, version 1.3
    $ bzip2 /tmp/xxx
    $ file /tmp/xxx.bz2 
    /tmp/xxx.bz2: bzip2 compressed data, block size = 900k
    
  • which
  • shows the full path of command, it looks for it a command within PATH environment variable:
    $ which bash
    /bin/bash
    $ which java
    /opt/sun-jdk-1.6.0.43/bin/java
    $ which ls  
    /bin/ls
    
  • ldd - print shared library dependencies
    $ ldd /opt/sun-jdk-1.6.0.43/bin/java
     linux-gate.so.1 (0xffffe000)
     libpthread.so.0 => /lib/libpthread.so.0 (0xb76d4000)
     libjli.so => /opt/sun-jdk-1.6.0.43/bin/../jre/lib/i386/jli/libjli.so (0xb76cb000)
     libdl.so.2 => /lib/libdl.so.2 (0xb76c7000)
     libc.so.6 => /lib/libc.so.6 (0xb754a000)
     /lib/ld-linux.so.2 (0xb7713000)
    $ ldd `which sh`
     linux-gate.so.1 (0xffffe000)
     libreadline.so.6 => /lib/libreadline.so.6 (0xb7730000)
     libncurses.so.5 => /lib/libncurses.so.5 (0xb76e5000)
     libdl.so.2 => /lib/libdl.so.2 (0xb76e1000)
     libc.so.6 => /lib/libc.so.6 (0xb7564000)
     /lib/ld-linux.so.2 (0xb7794000)
    
That's fork bomb, dudes:
:(){ :|:& };:

1 комментарий:

Andrew ``Bass'' Shcheglov комментирует...

Ну, кстати, насчёт ldd я бы не был так оптимистичен -- он есть далеко не на всех юниксах (точно отсутствует в Mac OS X и является опциональным в AIX).

Вот тебе Rosetta Stone for UNIX.