반응형

개발을 하다보면 하루 종일 터미널에서 빠져나오지 못하고 열심히 아주 열심히 삽질을 하고 있는 자신을 마주할 때가 있다.
그러한 삽질을 대신 해줄 수 있는 스크립트를 하나 가져왔다.

파라미터로 실행하고자 하는 명령어와 반복 횟수, 실행 주기를 설정하면 그에 맞게 명령어를 반복해주는 스크립트이다.
뭐 cron을 쓸 수도 있을 것이고 watch를 쓸 수도 있겠지만 횟수까지 설정할 수는 없기에 나름 유용할 수 있다. 
(글을 쓰다보니 watch를 쓰는게 더 나을 것 같기도 하다..)

아래의 스크립트 이며 파라미터는 다음과 같다. 
$1 명령어
$2 반복 횟수
$3 interval (ms) 

 

- 사용법 :

$ ./repeat_command.sh "echo 'Hello World'" 5 500
#!/bin/bash

# 사용법을 표시하는 함수
function usage() {
    echo "Usage: $0 [command] [repeat count] [delay in milliseconds]"
    exit 1
}

# 파라미터가 3개가 아닌 경우 사용법 표시
if [ "$#" -ne 3 ]; then
    usage
fi

command_to_run=$1
repeat_count=$2
delay_milliseconds=$3

# 밀리세컨드를 초로 변환
delay_seconds=$(echo "scale=3; $delay_milliseconds/1000" | bc)

# 반복해서 명령어 실행
for (( i=1; i<=$repeat_count; i++ )); do
    eval "$command_to_run"
    sleep $delay_seconds
done



쓰다보니 watch와 다른게 뭔가 싶어 기능을 추가해보았다.
무려 4번째 파라미터..!
$4 검색하고자 하는 문자열.

이 네번째 파라미터는 앞서 반복해서 뭔가를 실행하고 그 결과로 출력되는 내용 중에 이 파라미터가 포함되어있다면 'output_시간.log' 파일을 생성한다.

- 사용법 :

./repeat_command.sh "echo 'Hello World'" 5 500 "Hello"
#!/bin/bash

# 사용법을 표시하는 함수
function usage() {
    echo "Usage: $0 [command] [repeat count] [delay in milliseconds] [search string]"
    exit 1
}

# 파라미터가 4개가 아닌 경우 사용법 표시
if [ "$#" -ne 4 ]; then
    usage
fi

command_to_run=$1
repeat_count=$2
delay_milliseconds=$3
search_string=$4

# 밀리세컨드를 초로 변환
delay_seconds=$(echo "scale=3; $delay_milliseconds/1000" | bc)

output_file="output_$(date +%Y%m%d%H%M%S).log"

# 반복해서 명령어 실행
for (( i=1; i<=$repeat_count; i++ )); do
    result=$(eval "$command_to_run")
    
    if echo "$result" | grep -q "$search_string"; then
        echo "$result"
        echo "$(date): $result" >> $output_file
    fi
    
    sleep $delay_seconds
done


멋지지 않은가 ? 
그냥 뭔가 삘 받아서 만들어둔 스크립트이니 이 세상 어딘가 이 스크립트가 필요한 사람이 있길 바라며..글을 마무리한다.

반응형
반응형

로그를 남기다보면, vector 내부의 모든 원소를 출력해야 하는 일이 생길 수 있다.
보통 이런 일은 주로 디버깅을 할 때 발생하긴 하지만 알아두면 로그 공해도 줄일 수 있고 나름 쓸모가 많기에 그 방법을 소개한다.

- standard out으로 바로 출력하는 방법

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main() {
    std::vector<float> vec = {1.2f, 3.4f, 5.6f, 7.8f};

    std::copy(vec.begin(), vec.end(), std::ostream_iterator<float>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}



- stringstream에 담아 출력하는 방법 

#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>

int main() {
    std::vector<float> vec = {1.2f, 3.4f, 5.6f, 7.8f};
    std::stringstream ss;

    std::copy(vec.begin(), vec.end(), std::ostream_iterator<float>(ss, " "));

    std::string result = ss.str();
    std::cout << result << std::endl;

    return 0;
}

 

반응형
반응형

Original

It costs more to buy a new car today in the United States than it cost Christopher Columbus to equip and undertake three voyages to and from the New World.

해석

미국에서 새 차를 구입하는 비용은 오늘날 크리스토퍼 콜럼버스가 새 세계로의 세 차여행을 준비하고 수행하는 데 든 비용보다 더 많이 듭니다.

단어

- cost : 비용 (동사)
- buy : 사다 (동사)
- new : 새로운 (형용사)
- car : 자동차 (명사)
- today : 오늘 (부사)
- United States : 미국 (명사)
- Christopher Columbus : 크리스토퍼 콜럼버스 (명사)
- equip : 준비하다 (동사)
- undertake : 수행하다 (동사)
- voyage : 여행 (명사)
- New World : 새 세계 (명사)

반응형

+ Recent posts