Linux 사용시 파일 내부의 특정 표현을 수정하고자 하는 경우 sed 명령어를 활용할 수 있다.


sed 의 기본적인 형태는 아래와 같다.

sed <options> <address> <commands> <filename>

<options>

-n : suppress automatic printing (sed는 Matching 되는 Line 외에도 모든 Line 을 기본적으로 printing 하는데, line number 를 출력하고자 하는 경우(command =) 또는, Matching 되는 line 만 출력하고자 하는 경우(command /p)에는 -n option을 활용하면 된다.)

<address>

number : Match only the line number

/regular_expression/ : Match lines matching the regular_expression

addr1,addr2 : Match lines from addr1 to addr 2

addr,+N : Match addr and the following N lines

addr,~N : Match lines from addr to N*addr

<commands>

= : print the line number

/p : print the line

s/regular_expression/replacement : replace 


다만, 순전히 내 기준에서 가장 많이 사용하는 sed의 형태는 아래와 같다.

sed s/<regular_expression>/<replace>/g <file>

여기서 /g는 vim에서 replace 할 때의 /g 와 마찬가지로, 여러 개가 매칭 되는 경우 여러번 실행하도록 하는 옵션


sed 는 <file> 로 부터 <regular_expression> 을 찾아 <replace> 로 치환하여, 표준 출력을 해준다.

이는 vim 에서 replace 하는 것과 비슷하다.

결과물이 표준 출력 되기 때문에, 아래와 같이 > 또는 >> 를 이용해 파일에 써줄 수 있다.

다만, 여기서 파일이 이름이 같은 경우 아무것도 써지지 않는다는 점을 유의하자.

sed s/<regular_expression/<replace>/g <input_file> > <output_file>


아래는 예시

sed s/^$//g test.txt > test.txt.tmp

mv test.txt.tmp test.txt

(test.txt 파일로 부터 공백인 행을 없애 test.txt.tmp 파일에 출력한다.)


'Programming > Linux' 카테고리의 다른 글

Linux HDD mount  (0) 2018.04.06
HandBrakeCLI  (0) 2018.04.06
Linux find  (0) 2018.02.04
Ubuntu sources.list generation  (0) 2017.09.06
Ubuntu Redshift  (0) 2017.07.02

Linux system 에서 파일을 찾고자 할 떄,

find 함수를 사용하면 찾을 수 있다.


기본적인 사용법은 아래와 같다.

$ find <directory> -name <finding_file_name>


옵션으로 아래와 같이 사용 할 수 있다.

-type f : 파일

-type d : 폴더(directory)

-maxdepth 1 : find 는 기본적으로 하위 디렉토리까지 전부 검사 한다. maxdepth는 하위 몇 디렉토리 까지 검사할 것인지를 설정한다.

-and : 여러개의 조건에 대해 찾고자 할때 -and 옵션과 함께 조건을 추가할 수 있다.

-and ! : 해당 조건이 거짓인 경우에 대해 찾고자 할 때, -and ! 을 사용하면 된다.

-iname : 파일 이름의 대소문자를 고려하지 않고 검색한다.


아래는 예시

$ find . -type f -name 'abc*' -and ! -name 'abc'

(현재 디렉토리로 부터 abc로 시작하는 이름을 가진 파일 중에 파일 이름이 abc는 아닌 것을 찾는다.


xargs와 함께 사용하면, 

디렉토리에서 원하는 파일을 복사/삭제 하기 수월하다

예시

$ find . -type f -name 'abc*' -and ! -name 'abc' | xargs rm -f

(현재 디렉토리로 부터 abc로 시작하는 이름을 가진 파일 중에 파일 이름이 abc는 아닌 것을 찾아 삭제한다.

'Programming > Linux' 카테고리의 다른 글

HandBrakeCLI  (0) 2018.04.06
Linux sed  (0) 2018.02.05
Ubuntu sources.list generation  (0) 2017.09.06
Ubuntu Redshift  (0) 2017.07.02
Linux timezone  (0) 2017.06.20

Bash Script 에서 Script 에 관한 문법


Bash Script 에서 따로 자료형을 입력하지 않아도, initial value 가 string 형태의 경우 string 자료형으로 인식한다.


1. String Length

${#string} : string 의 길이 

expr length $string


2. Substring

${string:position} : string 의 앞에서 부터 position 의 위치 부터 string의 끝까지의 substring (position은 0부터 시작한다.)

${string:position:length} : string 의 앞에서 부터 position 위치 부터 length 길이의 substring


참조

http://tldp.org/LDP/abs/html/string-manipulation.html


'Programming > Bash Script' 카테고리의 다른 글

Bash Script Variable Split by Space  (0) 2018.02.04
Bash Script Echo  (0) 2018.02.04
Bash Script If statement  (0) 2017.07.04
Bash script argument  (0) 2017.06.20
Bash script sudo 권한 체크  (1) 2017.06.19

Bash Script If statement

Bash 스크립트 if 조건문 문법

기본적으로, if / elif / else / fi 로 구성한다. 

if[] bracket 사이에는 공백이 있어야 하며, [] bracket 과 [[ ]] double bracket( ) paranthesis 는 다를 수 있으므로 주의하자.

if [ expression ]

then

statement

elif [ expression ]

then

statement

else 

statement

fi


expression 에 사용하는 Comparison 은 아래와 같다.

integer comparison

if [ "$a" -eq "$b" ]  : 같음(equal to)

if [ "$a" -ne "$b" ]  : 다름(not equal to)

if [ "$a" -lt "$b" ]  : 작음(less than)

if (( "$a" < "$b" ))

if [ "$a" -gt "$b" ] : 큼(greater than)

if (( "$a" > "$b" ))

if [ "$a" -le "$b" ] : 작거나 같음(less than or equal to)

if (( "$a" <= "$b" ))

if [ "$a" -ge "$b" ] : 크거나 같음(greater than or equal to)

if (( "$a" >= "$b" ))


string comparison

if [ "$a" == "$b" ]  : 같음(equal to)

if [ "$a" = "$b" ] ※ = 양쪽 모두 빈 칸 이여야 함

if [ "$a" != "$b" ]  : 다름(not equal to)

if [ -z "$a" ] : string 이 null 이 아님(즉, 0이 아닌 길이가 있는 string)


if statement 에 not 을 사용하고자 하는 경우 ! 를 사용하면 된다.

if ! [ "$arg" -eq "matching" ]

then

echo "Not matched with the first one"

fi


If statement 를 사용해 file 존재 유무 확인

if [ -d <DIR> ] : Directory 가 존재

if [ -f <FILE> ] : 파일이 존재하며, directory 가 아님

if [ -x <FILE> ] : 파일이 실행 가능함

if [ -f test.c ]

then

echo "File test.c exists!"

else

echo "File test.c does not exist"

fi


If statement  Logical AND, OR 문법

if [[ condition1 && condition2 ]] : Logical AND

if [ condition1 ] && [ condition2 ]

if [ condition1 -a condition2 ]

if [[ condition1 || condition2 ]] : Logical OR

if [ condition1 ] || [ condition2 ]

if [ condition1 -o condition2 ]

if [[ condition1 && condition2 ]] || [[ condition3 && condition4 ]]

then

echo "Condition (1 AND 2) OR (3 AND 4)"

fi



참조

http://tldp.org/LDP/abs/html/comparison-ops.html

'Programming > Bash Script' 카테고리의 다른 글

Bash Script Variable Split by Space  (0) 2018.02.04
Bash Script Echo  (0) 2018.02.04
Bash Script String  (0) 2017.07.04
Bash script argument  (0) 2017.06.20
Bash script sudo 권한 체크  (1) 2017.06.19

Bash script 실행 할 때 Arguments 를 넣는 방법


C/C++ 에서 와 마찬가지로 Argument 순서대로 1, 2, 3, ... 으로 들어간다.(0번은 실행 스크립트 파일 이름)

달러+번호 가 해당 argument에 해당하며, argument 총 갯수는 $#으로 반환된다(여기서 argument의 총 갯수는 실행 스크립트 argument를 제외한 순수 argument 들의 갯수이다.)

실행 스크립트는 0번째 argument, 즉, $0가 된다.

if [ $# -ne 2 ]

then

echo "please input arguments"

echo "Usage:$0 <input1> <input2>

exit

fi


arg1=$1

arg2=$2


파일로 부터 읽어, Argument 를 전달 할 수 도 있다.

while read line

do 

echo $line

./script $line

done < $file


※ Bash script 내 에서 실행하는 다른 script 또는 다른 실행 파일로 print 를 하는 경우 argument 로 전달 될 수 있음


'Programming > Bash Script' 카테고리의 다른 글

Bash Script Variable Split by Space  (0) 2018.02.04
Bash Script Echo  (0) 2018.02.04
Bash Script String  (0) 2017.07.04
Bash Script If statement  (0) 2017.07.04
Bash script sudo 권한 체크  (1) 2017.06.19

Linux Time zone 변경 하는 방법


우선 date 명령어로 시스템의 시간을 확인하자

$ date

끝에 KST로 나와야 한국 시간


/etc/localtime 파일을 해당 타임존의 파일로 교체해 주면 된다. (binary 파일이라 열어도 알아볼 수는 없다.)

$ mv /etc/localtime /etc/localtime.bak

$ ln -s /usr/share/zoneinfo/Asia/Seoul /etc/localtime


'Programming > Linux' 카테고리의 다른 글

HandBrakeCLI  (0) 2018.04.06
Linux sed  (0) 2018.02.05
Linux find  (0) 2018.02.04
Ubuntu sources.list generation  (0) 2017.09.06
Ubuntu Redshift  (0) 2017.07.02

Linux Kernel programming 을 할 때는 일반적인 printf 함수를 사용 할 수 없다.

Kernel 에서 Debugging 등을 위해 print를 하고자 하는 경우

printk 함수를 사용할 수 있다.

헤더는 linux/kernel.h


사용법은 printf 함수와 유사하다.

printk("hello world\n");

printk("a=%d\n, a);

사용법은 비슷하지만, kernel에서 print 하는 것이기 때문에 print 된 것은 userspace 에서 보이지 않는다.

따라서, 프로그램 실행 후에 dmesg 를 통해 kernel message 를 확인해야 한다.

$ dmesg


printk 사용 시 floating point number 를 print 하는 데 문제가 생길 수 있다.

undefined reference to __aeabi_f2d 라고 나오는 것은 그 때문이라고 할 수 있는데,

Floting point 자체를 지원하지 않기 때문이다.

Kernel 버전 혹은 CPU 에 따라 kernel 내부에서 floating point 를 지원하는 경우도 있고, 지원하지 않는 경우도 있다.

'Programming > Linux Kernel' 카테고리의 다른 글

cross kernel debugging  (0) 2020.06.19
Linux kernel module compile from linux source  (0) 2019.01.17
Kernel module programming  (0) 2018.05.23
Kernel Version Check  (0) 2018.05.23

Bash script 를 sudo 권한으로 실행하는지 여부를 확인하고자 하는 방법은 아래와 같다.

if [ $EUID -ne 0 ]

then

echo "please run as root"

exit

fi

$EUID 는 Effective User ID 로 0 인 경우 root 를 나타낸다.


'Programming > Bash Script' 카테고리의 다른 글

Bash Script Variable Split by Space  (0) 2018.02.04
Bash Script Echo  (0) 2018.02.04
Bash Script String  (0) 2017.07.04
Bash Script If statement  (0) 2017.07.04
Bash script argument  (0) 2017.06.20

+ Recent posts