顯示具有 Linux 標籤的文章。 顯示所有文章
顯示具有 Linux 標籤的文章。 顯示所有文章

2021年7月28日 星期三

Dev Tips - command line snippets

 

Command - equal to ldd

objdump -p <path/to/program> | grep -i needed

Command - while loop with date and sleep

while [ 1 ] ; do date +"%M:%S" ; sleep 1 ; done

Command - cp

merge one folder to another folder

if file exists in dummy_tmp, it will replace by file in origin_tmp

$ cp -RT origin_tmp/ dummy_tmp/
$ diff -ur dummy_tmp origin_tmp

2021年1月21日 星期四

bash tips - 使用部分執行檔名來發出終止程式指令 terminate processes by partial process name

在 Linux 系統中,時常會遇到需要終止執行中的程式,如果只有單一執行檔程式執行多個 Instances 時,可簡單使用下列指令例如:

killall <process name>

但若是有多個 prefix 相同的程式就要分別指定

killall test_a test_b test_c

為了方便使用,初步寫一版可使用部分執行檔來終止程式的腳本,下例腳本是使用以下 /bin/sh 測試:

  • busybox v1.31
  • dash 0.5.8-2.10

Script example


#!/bin/sh
pname="$1"
if [ "x$pname" = x ]; then
echo "syntax: <partial process name>"; exit 1 ; fi
ps -a | grep "$pname" | \
grep -v grep | \
sed -e "s/^[ ]*\([0-9][0-9]*\) .*$/\1/g" | \
while read pid ; do kill $pid ; echo "terminate $pid" ; done

 Test output

~$ sleep 86400 &
[1] 2824
~$ sleep 86402 &
[2] 2832
​
sh 加上 -x 可除錯執行


~$ sh -x terminate_pname.sh sleep
+ pname=sleep
+ [ xsleep = x ]
+ ps -a
+ grep sleep
+ grep -v grep
+ sed -e s/^[ ]*\([0-9][0-9]*\) .*$/\1/g
+ read pid
+ kill 2824
+ echo terminate 2824
terminate 2824
+ read pid
+ kill 2832
+ echo terminate 2832
terminate 2832
+ read pid
[1]- Terminated              sleep 86400
[2]+ Terminated              sleep 86402
​

參考文章


2019年10月23日 星期三

[TIPS] 檢查CPU溫度 Raspberry PI


命令

 /opt/vc/bin/vcgencmd

CPU溫度


pi@raspberrypi:~ $ /opt/vc/bin/vcgencmd version

Jul  9 2019 14:40:53

Copyright (c) 2012 Broadcom

version 6c3fe3f096a93de3b34252ad98cdccadeb534be2 (clean) (release) (start)

pi@raspberrypi:~ $ uname -a

Linux raspberrypi 4.19.57-v7+ #1244 SMP Thu Jul 4 18:45:25 BST 2019 armv7l GNU/Linux

pi@raspberrypi:~ $ /opt/vc/bin/vcgencmd measure_temp

temp=45.1'C

2019年6月29日 星期六

Bash Tip - Why .bashrc (dot bashrc) not loaded after new user created?


Recently, I reinstalled ubuntu 16.04LTS on my desktop as a Linux server.

After a new user created, ssh into the server each time, but .bashrc (dot bashrc) not loaded after new user created? Please try to modify the profile as bellow.

Append some commands into ~/.profile

result of test-cases.sh example

~$ source .profile
load /home/ben/.bashrc by ~/.profile
.bashrc has been executed once

Appendix


2019年3月14日 星期四

Bash Tip - Run Command with multiple arguments with ssh




This is a note for logging my simple way to use ssh and bash with continuous integration on remote server.

You can execute the following test-cases.sh on your host.

It will execute remote script of the test with correct argument contents.

source code of test-cases.sh on host





directory layout of tester account on test-server



source code of tests.sh on test-server




result of test-cases.sh example

source ./test-cases.sh
➜  ~ run_cases "\"1\"\"" "b'\"" "c'#\"" "\"\"" "\'"
ssh test-server cd test-cases; /bin/bash ./tests.sh  "\"1\"\"" "b\'\"" "c\'\#\"" "\"\"" "\\'"
argv[1]=["1""]
argv[2]=[b\'"]
argv[3]=[c\'\#"]
argv[4]=[""]
argv[5]=[\\']

Notes

printf %q  is use to make safe quote the argument if hashtag(#), quote('), double quote(") and so on.

Appendix



2011年8月17日 星期三

自製作業系統 BenOS 原始碼公佈

bosv0dot2
最近把自製作業系統原始碼整理一下並修正部分問題,另附寫說明文件後,於今晚,發佈Ben OS v0.2,有興趣的朋友可從GitHub下載原始碼。
程式碼目前已在下列平台編繹並使用Qemu測試:
  • Ubuntu 10.04, 10.10, 11.04
  • Fedora 12
程式目錄說明
apps 應用程式,目前只有一個 bshell
blibc BOS用到的自製的C函式庫
boot 啟動程式
include 標頭檔案
kernel 系統核心相關
lib 核心相關函式庫
scripts 系統開發腳本檔
test 執行測試用
tools 預留工具程式
如何編繹請參閱?
Readme.md
Ubuntu + Qemu在VMWare Fusion Mac問題?
bosv0dot2-howtoquit
當Qemu執行時,跳離的快速鍵將會失效,造成無法正常使用Xwindow,可使用CTRL+ALT+2回到qemu monitor console,再輸入 "quit" + Enter 鍵離開Qemu 視窗。
BOS v0.2 編繹已不需要管理權限?
原來使用mount 磁片檔方式需要管理權限,現已修改為使用 mcopy (mtools)。(感謝社群成員Dennis 提供建議)
註:下次社群活動09/06/2011,詳請參閱社群首頁。
更新0821

2011年8月4日 星期四

FreeBSD 9.0 支援的新功能

Freebsd 7.2 VM boots

今天看到FreeBSD 9.0 Goes Into Beta一文,才發現我用FreeBSD 7.2已經有點老了。
以下是部分譯文,供大家參考:
  • 預設檔案系統為ZFS
  • 硬體相關
    • 支援SSD TRIM
    • 支援802.11n 高速模式
    • 更多儲存裝置ATA/CAM
    • 支援PCI hot-plug
  • 系統相關
    • 支援休眠模式(S4 hibernation)
    • 支援Linux 64-bit二進位檔在FreeBSD/amd64下,
    • 支援EFI 開機
    • 適當地支援 LLVM (the Low-Level Virtual Machine) 到下一版的 GCC, 並支援user-land D-Trace
    • 更快的開機
  • 虛擬化相關
    • Xen Dom0
    • 改進Oracle VM VirtualBox的相容性
更多請參考 FreeBSD.org Wiki
目前看到的預定發佈時間為2011-09;不過Beta1有些延遲,看來要再等等才會出了:細節請參考工程發佈資訊.

10-25 更新: 

  • RC1於2011-10-18 發佈,i386 ISO請至RC1協助下載測試,其他ISO請參考協助測試
11-30 更新: 

  • RC2於2011-11-12 發佈,i386 ISO請至RC2協助下載測試,其他ISO請參考協助測試

2012-01-30 更新: 
  • 正式版於2012-01-12 發佈,i386 ISO請至REL協助下載測試,其他ISO請參考協助測試





2011年7月31日 星期日

如何在Ubuntu/Linux中使用 Objective C + Blocks?



接續上篇"Objc Hello範例: Ubuntu上使用GNUstep and gobjc"一文。
當使用Blocks 語法時,卻發現不能使用,原因是Blocks 並未正式通過GNU GCC允許加入內建;
在不斷實驗並尋找後,終於找到一個解法:clang + BlocksRuntime。
BlocksRuntime 是 compiler-rt 專案的一部分。
首先使用svn指令下載: svn checkout http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
接著依序來編繹 ./BlocksRuntime/libBlocksRuntime.so
    1. cd compiler-rt/BlocksRuntime/
    2. 建立 ./config.h 包含下兩行內容
        #define HAVE_SYNC_BOOL_COMPARE_AND_SWAP_INT 1
        #define HAVE_SYNC_BOOL_COMPARE_AND_SWAP_LONG 1
    3. make
demoblocks 程式編繹
  - make run prefix=<你的路徑中包含compiler-rt>
  (註: 筆者的環境中已將 "BlocksRuntime/" 目錄複製到demoblocks 目錄中)
demoblocks範例:
main.m
#include <stdio.h>
#include "Block.h"
typedef int(^ablock)(void);
ablock dosth(int difference)
{
        __block int a = 1;
        int b = difference;
        return Block_copy(^{
                a += b;
                return a;
        });
}
int main()
{
        ablock a = dosth(10);
        int i;
        for (i = 0 ; i < 3; i++) {
                printf("%d\n", a());
        }
        Block_release(a);
        return 0;
}

Makefile
prefix?=.
BLOCKS_RT_DIR=$(prefix)/BlocksRuntime/
BLOCKS_RT= -I$(BLOCKS_RT_DIR) -L$(BLOCKS_RT_DIR) -lBlocksRuntime
all: demoblocks
demoblocks: main.m
        clang $(BLOCKS_RT) -fblocks $^ -o demoblocks
run: demoblocks
        export LD_LIBRARY_PATH=$(BLOCKS_RT_DIR) ; \
        ./demoblocks
clean:
        rm -f ./demoblocks
執行結果:make run prefix="./compiler-rt"

clang -I./BlocksRuntime/ -L./BlocksRuntime/ -lBlocksRuntime -fblocks main.m -o demoblocks
export LD_LIBRARY_PATH=./BlocksRuntime/ ; \
        ./demoblocks
11
21
31
由此下載範例源始碼。
可參考Blocks 相關的文章:

2011年7月28日 星期四

Objc Hello範例: Ubuntu上使用GNUstep and gobjc


最近使用Objective-C越順手,想在Ubuntu環境中也能寫,
以及使用Mac/iOS 的Foundation函式,經參考 GNUstep on Linux 這篇文章,記錄以下心得。

環境安裝 Ubuntu 10.10:

  • sudo apt-get install gobjc 
  • sudo apt-get install gnustep
  • sudo apt-get install gnustep-devel


範例:
hello.h, hello.m and main.m
#include <foundation foundation.h>
@interface Hello: NSObject {
}
- (void) show;
@end

@implementation Hello

- (id) init
{
self = [super init];
NSLog(@"%s", __FUNCTION__);
return self;
}
- (void) show
{
NSLog(@"Hello GNUObjc with GNUstep");
}
- (void) dealloc
{
NSLog(@"%s", __FUNCTION__);
[super dealloc];
}
@end

int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Hello *obj = [[Hello alloc] init];
[obj show];
[obj release];
[pool drain];
return 0;
}

Makefile

STEPFLAGS= $(shell gnustep-config --objc-flags) -lgnustep-base
OBJCFLAGS= -g $(STEPFLAGS) -O
OBJCFILES= hello.m main.m
OBJS = $(OBJCFILES:.m=.o)

.SUFFIXES: .m .o

all: hello

.m.o:
$(CC) $(OBJCFLAGS) -c $<

hello: $(OBJS)
$(CC) -o $@ $^ -lobjc -lgnustep-base

run: hello
./$^

clean:
rm -f *.o *.d hello

執行結果:

2011-07-28 21:53:25.540 hello[12151] -[Hello init]
2011-07-28 21:53:25.541 hello[12151] Hello GNUObjc with GNUstep
2011-07-28 21:53:25.541 hello[12151] -[Hello dealloc]

請由此下載範例源始碼。

(ps: 圖片來源為http://wiki.gnustep.org 所有)

2010年11月6日 星期六

Ubuntu warning: setlocale: LC_CTYPE: cannot change locale

今日發現使用Ubuntu Terminal 會一直出現下列警告訊息
使用版本: 10.04
 warning: setlocale: LC_CTYPE: cannot change locale (en_US)
最後求助google 大神才找到解決方法:


執行下列指令:
 $ sudo locale-gen en_US.UTF-8
 $ sudo update-locale LANG=en_US.UTF-8
就恢復正常。

參考:
locale problem threads on ubuntuforums

2010年1月24日 星期日

User Mode Linux - CoLinux on Windows 使用心得

之前, 我習慣使用 VMWARE來執行 Ubuntu 或 Fedora來做。
也曾聽過 User Mode Linux, 但一直沒有玩看看。

最近稍微實驗了一下, Win32 平台的 coLinux (Cooperative Linux) 果然很容易使用!!!

分別下載 coLinux-0.7.5.exe 及 WinPcap_4_1_1.exe
因為 coLinux 使用 WinPcap來實現網路功能,  故需要個別安裝 WinPcap
另外需要下載FileSystem image, 我下載了 Ubuntu-9.04-1gb.7z

看了一下操作說明, 將 coLinux 安裝至 C:\coLinux,
且解壓Ubuntu-9.04-1gb.7z 中Ubuntu-9.04.ext3.1gb.fs 到同一目錄。

我建立一個命令批次檔 C:\coLinux\run_ubuntu.bat
 內容如下:
cmd /K "colinux-daemon kernel=vmlinux initrd=initrd.gz cobd0=Ubuntu-9.04.ext3.1gb.fs eth0=slirp root=/dev/cobd0"
 
執行後
 


接著執行C:\coLinux\colinux-console-nt.exe
 

緊接著在colinux login: 輸入 root 及密碼也是root
 
最後我完成一個精簡的User Mode Linux 的 Ubuntu 環境。

  • 其他可供參考內容, 使用 linux 環境
Jserv 演講:User-Mode Linux 應用簡介
http://blog.linux.org.tw/~jserv/archives/001871.html