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

2015年9月1日 星期二

mv, rm, touch 更動特殊名稱檔案( mv 減減 )

新增檔名 "--bear"的檔案
直接touch會報錯
$ touch --bear
touch: unrecognized option '--bear'
Try 'touch --help' for more information.
應使用
$ touch -- --bear

移動檔名"--bear"的檔案
直接mv會報錯
$ mv --bear __bear
mv: unrecognized option '--bear'
Try 'mv --help' for more information.
應使用
$ mv -- --bear __bear

刪除檔名"--bear"的檔案
直接rm會報錯
$ rm --bear
rm: unrecognized option '--bear'
Try 'rm ./--bear' to remove the file ‘--bear’.
Try 'rm --help' for more information.
應使用
$ rm -- --bear


參考資料:
http://superuser.com/questions/361573/mv-command-confuses-directory-name-with-command-option  mv command confuses directory name with command option


2015年1月7日 星期三

svn merge log的測試shell script

承前篇 Arch啟用http存取svn和svn搬移log到另一個repos下
後來寫了個建構環境和merge log的script:
建立該篇文章的環境:
用法:
$ bash create_env.sh /home/svn/ #在 /home/svn/ 下建立test和test2的repos,並建立那篇文章的資料結構
#!/bin/bash
# $ bash create_env.sh 
# eg. # bash create_env.sh /home/svn/

SVN_REPOS_PATH=$1
cd ${SVN_REPOS_PATH}
svnadmin create test
svnadmin create test2

# cook test repos
svn checkout file://${SVN_REPOS_PATH}test test_checkout
cd test_checkout
mkdir trunk tags branches
touch trunk/hello.txt
svn add *
svn commit -m "hello test"
svn update
svn log
cd ..

# cook test2 repos
svn checkout file://${SVN_REPOS_PATH}test2 test2_checkout
cd test2_checkout
mkdir trunk tags branches
mkdir -p trunk/media/cloud/web/internet
svn add *
svn commit -m "hello test2"
svn update
svn log
cd ..

rm -rf test_checkout test2_checkout
ls -l

merge log:
用法:
# bash test_merge_log.sh /home/svn/test /home/svn/test2 trunk/media/cloud/web/internet # 把test的log搬到test2的 trunk/media/cloud/web/internet 資料夾下
#!/bin/bash
# merge REPOS1 log to REPOS2 folder
# run it as root
# # bash test_merge_log.sh   
# eg. # bash test_merge_log.sh /home/svn/test /home/svn/test2 trunk/media/cloud/web/internet

SVN_REPOS1_PATH=$1
SVN_REPOS2_PATH=$2
REPOS2_FOLDER=$3

svnadmin dump ${SVN_REPOS1_PATH} > test.dump
cat test.dump | svndumpfilter include "trunk" --drop-empty-revs --renumber-revs > test_trunk.dump
find . -name test_trunk.dump | xargs perl -ne 'open NEW, ">>test_trunk_new.dump"; s/trunk\///g; print NEW'
svnadmin load ${SVN_REPOS2_PATH} --parent-dir ${REPOS2_FOLDER} --ignore-uuid < test_trunk_new.dump
rm *.dump

# check
svn checkout file://${SVN_REPOS2_PATH} test2_checkout
cd test2_checkout
svn log
cd ..
rm -rf test2_checkout

原始碼:
https://github.com/kalecgos0616/tools/tree/master/svn

注意:
要在該tty 也cd到資料夾要用 . xxx.sh (要多一個點),否則用bash xxx.sh不會換目錄,但仍然會在該目錄執行指令

The reason is that each process has its own current directory, and when you execute a program from the shell it is run in a new process. The standard "cd", "pushd" and "popd" are builtin to the shell interpreter so that they affect the shell process.
翻譯:
每個process 擁有自己的資料夾,當你執行一個shell程式時等於開了一個新的process。"cd"那些指令被內置到shell解釋器,仍影響shell進程。使用source(.) 會讓該process在現在的tty環境執行

參考資料:
http://stackoverflow.com/questions/874452/change-current-directory-from-a-script

2014年12月19日 星期五

arch 開機執行程式

前言:
每次重開機後,主機與serial port的stty設定都會跑掉,所以想在開機時去設定(重新插拔USB接口設定不會跑掉)

https://raymii.org/s/tutorials/rc.local_support_on_Arch_Linux_and_systemd.html
1. 編輯 /etc/rc.local (如果沒有就自己建一個)
2. 把 /etc/rc.local 權限設成 755,注意:因為是bash,最前面要寫 #!/usr/bin/bash,例:
#!/usr/bin/bash
touch /tmp/1
stty -F /dev/ttyUSB1 raw speed 9600 -crtscts cs8 -parenb -cstopb

3. 編輯 /usr/lib/systemd/system/rc-local.service:
[Unit]
Description=/etc/rc.local compatibility

[Service]
Type=oneshot
ExecStart=/etc/rc.local
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

4. 執行 下面這個指令,他會產生一個symlink 到 /etc/systemd/system/multi-user.target.wants/ 下面
# systemctl enable rc-local.service
Created symlink from /etc/systemd/system/multi-user.target.wants/rc-local.service to /usr/lib/systemd/system/rc-local.service.

5. 手動檢測:
# systemctl start rc-local.service

實測結果:
重開機後
touch /tmp/1 會正常執行(執行身份是root),但stty 把他印到/tmp/1 (  stty >> /tmp/1 )則是一片空白,這可能要看開機message... 這挺複雜的....
這位網友遇到同樣的問題: http://www.gobsd.org/viewtopic.php?f=4&t=3239
最後暫時用每次呼叫web api時都設定stty,這會讓使用者每次多等0.3s
之後可以嘗試用cronjob看看

參考資料:
http://stackoverflow.com/questions/18903512/set-up-serial-on-start-up-raspberry-pi # Raspberry Pi 在rc.local設定stty的答案被採納