Tuesday, August 23, 2016

[Linux,Trouble shooting] /bin/sh^M: bad interpreter: No such file or directory



When you create a script file on Windows and then run it on Linux,
an error occurs as follows:

     /bin/sh^M: bad interpreter: No such file or directory

It is because the 'new line character' is different. 


< Solution >

1. vi -b
: go to the binary mode of the vi
-> Then, you could see the character ^M
-> Delete this.


2. dos2unix   filename.txt
: Modify the file with the command on Linux console




============================================================================
Windows 에서 script file을 작성하고 Linux에서 file을 실행하면 아래와 같은 에러가 발생하는데, 이는 개행문자 (줄바꿈 문자)가 다르기 때문이다

     /bin/sh^M: bad interpreter: No such file or directory


< 해결 방법 >

1. vi -b
: vi 의 바이너리 모드로 들어가서 ^M 이라는 문자를 지워준다.


2. dos2unix   filename.txt
: Llinux console에서 위의 명령어로 파일을 변경한다.

Monday, August 22, 2016

[Linux] (Clipping / 퍼옴) 대소문자 변환 / How to substitute uppercase and lowercase characters in a file

 

< How to substitue uppercase and lowercase characters >


* Uppercase -> lowercase characters   in   'filename.txt'
 
tr [:lower:] [:upper:] < filename.txt


* Lowercase -> uppercase characters   in   'filename.txt'
tr [:upper:] [:lower:] < filename.txt

 
* Saving results as a file

tr [:upper:] [:lower:] < filename.txt > filename_result.txt


* Substitute characters of strings in variables
sample_string="This Is A Sample String"
echo "$sample_string" | tr [:upper] [:lower:] 
 
 
==========================================================================================

< 파일에서 대문자 -> 소문자 / 소문자 -> 대문자로 치환/변환하는 방법 >

 
sample.txt 파일의 소문자를 전부 대문자로 바꾸는 경우
tr [:lower:] [:upper:] < sample.txt
반대로 대문자를 전부 소문자로 바꾸는 경우
tr [:upper:] [:lower:] < sample.txt

위와 같이 하면 바뀐 결과가 화면에 출력된다. 파일로 저장하고 싶으면 아래와 같이 하면 된다. sample.txt 파일에서 대문자를 모두 소문자로 바꾸고 이 결과를 sample.lower.txt에 저장하는 방법이다. 
tr [:upper:] [:lower:] < sample.txt > sample.lower.txt

스크립트에서 특정 변수에 할당된 문자열에 적용하는 것도 간단하다. 변수 sample_string에 할당된 문자열을 모두 소문자로 변환하고 싶다면 아래의 예시를 참고하자.
sample_string="This Is A Sample String"

echo "$sample_string" | tr [:upper] [:lower:]

[Linux, shell script] read a line or column from a file

< Read line by line from a file >

 filename.txt
-----------------------
aaa 111 AAA
bbb 222 BBB
ccc 333 CCC
-----------------------


Shell script
-----------------------#!/bin/bash

while read line
do

    echo "-> ${line}"

done < filename.txt
-----------------------

Result
------------------------> aaa 111 AAA
-> bbb 222 BBB
-> ccc 333 CCC
-----------------------








 

< Read column by column from a file >


filename.txt
-----------------------
aaa 111 AAA
bbb 222 BBB
ccc 333 CCC
-----------------------


Shell script
-----------------------#!/bin/bash

while read col1 col2 col3
do

    echo "${col1}/${col2}/${col3}"

done < filename.txt
-----------------------
 


Result
-----------------------aaa/111/AAA
bbb/222/BBB
ccc/333/CCC
-----------------------

[Linux command] sort option

 

sort [option]

 

-r: in descending order / 내림차순 정렬

-k: sorting by the designated column / 지정한 열을 기준으로 정렬

e.g. sort -k 3: sorting by the 3rd colum / 3번째 열을 기준으로 정렬

Thursday, May 26, 2016

[Linux, Command] Delete folder



Deleting all (sub) folders and files

> sudo rm -rf  folderName



  • -f = to ignore non-existent files, never prompt
  • -r = to remove directories and their contents recursively
  • -v = to explain what is being done

Monday, April 18, 2016