bash shell 프롬프트용 변수

Posted at 2010/04/16 13:41 // in Tips // by Daniel

bash man page에서 퍼옴.

PROMPTING
       When executing interactively, bash displays the primary prompt PS1 when
       it is ready to read a command, and the secondary  prompt  PS2  when  it
       needs  more  input  to  complete  a  command.  Bash allows these prompt
       strings to be customized by inserting  a  number  of  backslash‐escaped
       special characters that are decoded as follows:
              \a     an ASCII bell character (07)
              \d     the  date  in "Weekday Month Date" format (e.g., "Tue May
                     26")
              \D{format}
                     the format is passed to strftime(3)  and  the  result  is
                     inserted  into the prompt string; an empty format results
                     in a locale‐specific time representation.  The braces are
                     required
              \e     an ASCII escape character (033)
              \h     the hostname up to the first ‘.’
              \H     the hostname
              \j     the number of jobs currently managed by the shell
              \l     the basename of the shell’s terminal device name
              \n     newline
              \r     carriage return
              \s     the  name  of  the shell, the basename of $0 (the portion
                     following the final slash)
              \t     the current time in 24‐hour HH:MM:SS format
              \T     the current time in 12‐hour HH:MM:SS format
              \@     the current time in 12‐hour am/pm format
              \A     the current time in 24‐hour HH:MM format
              \u     the username of the current user
              \v     the version of bash (e.g., 2.00)
              \V     the release of bash, version + patch level (e.g., 2.00.0)
              \w     the  current  working  directory,  with $HOME abbreviated
                     with a tilde
              \W     the basename of the current working directory, with $HOME
                     abbreviated with a tilde
              \!     the history number of this command
              \#     the command number of this command
              \$     if the effective UID is 0, a #, otherwise a $
              \nnn   the character corresponding to the octal number nnn
              \\     a backslash
              \[     begin  a sequence of non‐printing characters, which could
                     be used to embed a terminal  control  sequence  into  the
                     prompt
              \]     end a sequence of non‐printing characters

 

예를 들어

$export PS1='\s−\v\$ '
-bash−3.2$

$ export PS1='\T−\v\$ '
01:28:29−3.2$

$ export PS1='\W \@\$ '
~ 01:40 PM$

크리에이티브 커먼즈 라이센스
Creative Commons License

windows에서 bash 셸 사용

Posted at 2009/11/02 23:59 // in Tips/Utility programs // by Daniel
일단 간단하게 쓰려고 찾아봤습니다. 물론 Cygwin을 쓰면 다들 해결되긴 하지만.. 그걸 깔긴 싫고 해서

http://win-bash.sourceforge.net/ 이 있더군요

적당한 폴더(저의 경우D:\programs)에 카피 한다음에 더블클릭하면 실행됩니다.

그리고 나서 UnixUtils를 받아서 http://unxutils.sourceforge.net/
적당한 폴더에 풀었습니다(실행파일들이 꽤 있습니다. 옛날 툴이라 버그나 기능미비가 좀 있지만)

제가 이걸 한 이유가 mplayer녹화기능을 쓰는 스크립트를 쓰기위한 거라서
저는 mplayer도 다운받아 압축풀었습니다.

저의 경우 다음과 같은 배치파일로 실행했습니다.
D:\programs에 bash 압축 풀고, d:\programs\UnixUtils, D:\programs\mplayer 존재


set Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\UniUSB\Common;D:\programs;D:\programs\UnixUtils\usr\local\wbin;D:\programs\mplayer
win-bash.exe
윈도 도스창에서 변수설정이 기억나질 않아서 그냥 썼네요.

기능 미비점이 꽤 되지만 제가 원했던 스크립트는 실행되는군요.
셸 스크립트 실행시에 앞줄에 #!win-bash.exe 해줘야 됩니다. ^^
사용자 삽입 이미지

win-bash로 셸 스크립트 실행 및 mplayer 실. 스크립트 앞줄에 #!win-bash.exe 해줘야 됩니다.

(저는 wget도 받아다가 실행합니다. 편리 ^^)
크리에이티브 커먼즈 라이센스
Creative Commons License

bash wait 명령어

Posted at 2009/02/11 11:15 // in Tips/Utility programs // by Daniel

bash 스크립트에서 백그라운드로 실행시키는 스크립트가 있을 때 그 스크립트의 종료를 기다려 처리하고 싶은 일이 있을 때가 있죠

그때 쓰는 게 wait입니다.

#!/bin/sh
./sort_db.sh &
echo "1st Line"
./bkptmp.sh &
echo "2dn line"
wait
echo "Some operation will follow this"

이런식으로 쓰면 됩니다. &로 백그라운드 실행한 스크립트를 기다려줍니다.

http://unstableme.blogspot.com/2008/06/bash-wait-command.html

Task contention상황에서 QoS를 위한 연구를 하는 중인데 스크립트로 돌리려니 이게 필요해서 찾아봤습니다.

크리에이티브 커먼즈 라이센스
Creative Commons License

bash에서 for문을 c언어처럼 쓰기

Posted at 2008/04/16 15:24 // in Tips // by Daniel

#!/bin/bash
for i in `seq 1 10`;
do
echo $i
done

#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done


#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done


#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done



산술계산

echo $((1+1))

echo $[1+1]


복잡한 계산은 bc를 사용한다

"echo 3/4 | bc -l"

크리에이티브 커먼즈 라이센스
Creative Commons License

Redirection

Posted at 2008/04/10 14:12 // in Tips // by Daniel

http://www.cpqlinux.com/redirect.html

  • The following command saves stdout and stderr to the files "out.txt" and "err.txt", respectively.
    [root@server /root]# ./cmd 1>out.txt 2>err.txt
  • The following command appends stdout and stderr to the files "out.txt" and "err.txt", respectively.
    [root@server /root]# ./cmd 1>>out.txt 2>>err.txt
  • The following command functions similar to the above two commands, but also copies stdout and stderr to the files "stdout.txt" and "stderr.txt", respectively.
    [root@server /root]# (((./cmd | tee stdout.txt) 3>&1 1>&2 2>&3\
    |tee stderr.txt) 3>&1 1>&2 2>&3) 1>out.txt 2>err.txt

    Note: Lines that end in a backslash are continued on the next line. Any such lines should be keyed in as one complete line. The lines are too long to fit on the web page without formatting them this way.

  • Capturing stdout
    The following will capture a copy of stdout and save it to a file called "stdout.txt"
    [root@server /root]# ./cmd | tee stdout.txt

    stdout goes through the pipe and tee is able to save a copy of it to the file "stdout.txt"; however, we just lost control of stderr. stderr will not go through the pipe, instead it goes directly to our display.

  • Gaining control of stderr and stdout.
    Lets gain control again of stderr and stdout. We do this by surrounding our command with a set of parenthesis.
    [root@server /root]# (./cmd | tee stdout.txt)

    Capturing stderr
    Now that we have swapped our stdout and stderr, lets hook up tee once again. tee will now capture stderr (tee believes that it is really stdout because stdout is the only thing that can come through the pipe).

    [root@server /root]# (./cmd | tee stdout.txt) 3>&1 1>&2 2>&3 \
    | tee stderr.txt

     

  • 크리에이티브 커먼즈 라이센스
    Creative Commons License