internal shell variable $IFS (Internal Field Separator). 

Its defaults to whitespace, tab and new line.


e.g.)

$ EXAMPLE="           a           b             c            "

$ echo $EXAMPLE

a b c

$


in bash script, we can just specify the 'IFS' as follows to preserve the whitespace

$ IFS='%'



WRITTEN BY
RootFriend
개인적으로... 나쁜 기억력에 도움되라고 만들게되었습니다.

,

출처 : http://stackoverflow.com/questions/12821715/convert-string-into-integer-in-bash-script



Constants with a leading 0 are interpreted as octal numbers.

You can remove the leading zero by parameter expansion:

hour=${hour#0}

or force base-10 interpretation:

$((10#$hour + 1))



WRITTEN BY
RootFriend
개인적으로... 나쁜 기억력에 도움되라고 만들게되었습니다.

,

bash shell 자체는 integer arithmetic 만 지원. bc 를 이용해야함


example)

LINK_UTIL=`printf "%.10f\n" $(bc -l <<< "scale=10; ${A}/${B}") `


WRITTEN BY
RootFriend
개인적으로... 나쁜 기억력에 도움되라고 만들게되었습니다.

,


출처 : http://stackoverflow.com/questions/3055755/equivalent-of-file-line-in-bash


echo $LINENO

echo `basename $0`



WRITTEN BY
RootFriend
개인적으로... 나쁜 기억력에 도움되라고 만들게되었습니다.

,

출처 : http://www.cyberciti.biz/faq/shell-how-to-determine-the-exit-status-of-linux-and-unix-command/



Shell: How to determine the exit status of Linux and UNIX command


Q. Can you explain the exit status of shell and commands under Linux / UNIX operating system?

A. All UNIX and Linux command has a several parameters or variables that can be use to find out the exit status of command. Please note that these parameters or variables may only be referenced assignment to them is not allowed. You can use $? to find out the exit status of command. $? always expands to the status of the most recently executed foreground command or pipeline. For example, you run the command cal:
$ cal
Now to see exit status of cal command type following command:
$ echo $?
Output:

0

Zero means command executed successfully, if exit status returns non-zero value then your command failed to execute. For example run command called cyberciti
$ cyberciti
Output:

bash: cyberciti: command not found

Display exit status of the command:
$ echo $?
Output:

127

Value 127 (non-zero) indicates command cyberciti failed to execute. You can use exit status in shell scripting too. You can store result of exit status in variable. Consider following shell script:

#!/bin/bash
echo -n "Enter user name : "
read USR
cut -d: -f1 /etc/passwd | grep "$USR" > /dev/null
OUT=$?
if [ $OUT -eq 0 ];then
   echo "User account found!"
else
   echo "User account does not exists in /etc/passwd file!"
fi
 

Save and execute the script as follows:
$ chmod +x script.sh
$ ./script.sh

Output:

Enter user name : jradmin
User account does not exists in /etc/passwd file

Try it one more time:
$ ./script.sh
Output:

Enter user name : vivek
User account found

As you can see, I have used grep command to find out user name stored in USR variable. If grep command finds user name in /etc/passwd command output it would return exit status of zero. This is stored in OUT variable. Next, if command makes decision based upon exit status stored in OUT variable.


WRITTEN BY
RootFriend
개인적으로... 나쁜 기억력에 도움되라고 만들게되었습니다.

,

readlink -f 상대경로





WRITTEN BY
RootFriend
개인적으로... 나쁜 기억력에 도움되라고 만들게되었습니다.

,

./example/

./example//

/usr/local/example

/usr/local/example


과같이 절대/상대 경로애서 디렉토리 이름만 취하고 싶은 때가 있는데.


bash 쉘 스크립트로 복잡하게 할수도 있지만


아주 간단한걸 찾았다. basename 이라는 명령어를 사용하면된다.


예)

CURRENT=`pwd`
BASENAME=`basename $CURRENT`

echo $BASENAME

exit;



WRITTEN BY
RootFriend
개인적으로... 나쁜 기억력에 도움되라고 만들게되었습니다.

,