看 Linux Shell 程式設計實力養成 的小筆記
背景執行:指令後面加上一個"&"
xclock&
指令一;指令二
不論結果如何,指令一執行後,接著執行指令二
指令一&&指令二
指令一正確執行後,接著執行指令二,若指令一錯誤,則不執行指令二
指令一||指令二
指令一正確執行後,不執行執行指令二,若指令一錯誤,則執行指令二
執行其他指令方法
(1)C語言源碼
使用tar指令解開,進入解開目錄後,依序執行如下指令:
./Configure
make
make install
執行後,編譯的執行檔通常放在/usr/local/服務名稱/bin/之下
(2)Shell Script
執行Shell Script的方式有三,分述如下:
賦予執行權限(chmod 755 script)後,直接執行(./Script)
執行Source script.sh(注意副檔名必須是sh)
執行. script.sh(. 與檔名間,要有空格,副檔名必須是sh)
執行 bash script.sh(與Shell Script及. script.shv相似,唯一的差別是會在另一個Bash環境中執行Script,執行Script所產生的環境變數與原來指令操作環境的變數不會共用
輸入輸出導向
cmd < file_name
cmd從file_name毒入資料
cmd> output_file <input_file
cmd從iput_file毒入資料,處理完畢後,處理完畢後再將資料寫回 output_file
導向空集合
ls file1 file2 >/dev/null
導向其他終端機
cat> /dev/pts/0 -->只需在TTY裝置前加上 /dev/即可
特殊字元
萬用字元 *
單一字元 ?
跳脫字元 \
跳脫字元可以用來去除字元的特殊涵義,例如:
#echo $PATH
/usr/local/sbin:/usr/local/bin
#echo \$PATH
$PATH
跳脫字元加上特定英文數字,則會有另外含意
\n 換行
\r Enter
\v 垂直對齊
\a alert(發出聲音)
\Oxx 轉換成八進位 ASCII 碼
跳脫字元放在指令列最後,代表這行指令尚未輸入完畢,會接續下一行的內容
單引號
#echo '$tmpdate'
$tmpdate
雙引號
雙引號類似單引號,保留部分字面意義(可以使用部分特殊符號 $,",\)
#echo "Let's play ball"
Let's play ball
#echo "$PATH"
/usr/local/sbin:/usr/local/bin
字元集合(中括號)
[a-z]:所有小寫英文字母
[0-9]:所有數字
[]呈現的是集合中的單一字元,例如[abc]表示a或是b或是c,而[abc][12]表示a1,a2,b1,b2,c1或是c2
[]字頭加上!,就是非的意思,例如:[!n-p36BY]就是指不包含n,o,p,3,6,B以及Y
括號擴展(大括號)
{vs,wu,pro}ftp 表示 vsftp,wuftp,proftp
#echo {a,b,c}{1,2}
a1,a2,b1,b2,c1,c2
#echo {1..10}
1 2 3 4 5 6 7 8 9 10
#echo {1..10..3}
1 4 7 10
grep 可尋找檔案中的字串,使用root搜尋會找出內文中所有包含root的行,但若加上^root,則僅會顯示 root 開頭的行
#grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
搜尋字尾符號為:的行
在字元後面加上$字元,代表該字元必須是結束符號(該行最後一個字元)才符合條件
#grep ;$ /etc/group
jack:x:100:
得到一個以c開頭且以h結尾的所有5位元字元的英語單詞
grep '\<c...h\>' /usr/share/dict/words
catch
clash
cloth
影響系統或服務運行的指令
halt 關閉點腦
init 改變系統的運行等級
init 0 關機
init 6 重新啟動
iptables 資料包處理與安全管理的指令
kill 傳送資訊給程序
pkill 傳送信號給指定的程序
reboot 重新啟動電腦
service 開啟或關閉服務
servie sshd stop 關閉sshd而無法遠端連機
shutdown 關閉機器
影響資料儲存的指令
cfdisk 設定硬碟分割區
e2fsck 檢驗 ext2 與 ext3 檔案系統
rm 刪除檔或目錄
swapoff 關閉指定的交換區空間
umount 卸載檔案系統
要知道目前所使用的shell
#echo $SHELL
/bin/bash
如果要設定環境變數指令為 export 變數名(不可加上$)
參數變數
. test1.sh arg1 arg2 arg3,所以
$1=arg1
$2=arg2
$3=arg3
$@=test1.sh arg1 arg2 arg3
$#=這函數被執行的次數
管線命令 | (Pipe)
管線命令僅會處理standard output,對於standard error output 會予以忽略
管線命令必須要能夠接受來自前一個指令的資料成為standard iput 繼續處理才行
擷取命令:
cut
cut -d '分隔字元' -f fields
cut -c 字元間隔
grep
grep [-acinv][--color=auto] '搜尋字串' filename
-a:將binary檔案以text檔案方式加以搜尋
-c:計算找到'搜尋字串'的次數
-i:忽略大小寫
-n:順便輸出行號
-v:反向選擇
排序指令
sort
重複資料只出現一次
uniq [-ic]
-i:忽略大小寫
-c:進行計數
知道多少行,多少字
wc
wc [-lwm]
-l:僅列出行
-w:僅列出多少字(英文單字)
-m:多少字元
雙向導向
tee [-a] file
-a:累積進 file 中
同時將資料流分別送到檔案及螢幕
刪除一段訊息當中的文字,或者進行文字訊息的替換
tr [-ds] SET1
-d:刪除訊息當中的SET1這個字串
-a:取代掉重複的字元
^符號在[]內代表反向選擇,在[]之外則代表定位在行首的意義
grep -n '\.$' aa.txt
.有其他意義(.代表一定有個任意字元,*代表重複前一個字元,0到無窮多次),必須使用\來加以解除其特殊意義
grep -n '^$' aa.txt
找出空白行
努力中...
2017年6月23日 星期五
2017年6月9日 星期五
[C#]Interprocess Communications (message queue)
搞好久,找到範例了...
但是使用 toolbox 拉的 messagequeue1 就是不會動....
不錯得範例
https://dotblogs.com.tw/calm/2013/12/01/132098
但是使用 toolbox 拉的 messagequeue1 就是不會動....
不錯得範例
https://dotblogs.com.tw/calm/2013/12/01/132098
2017年2月12日 星期日
[Excel]histogram 直方圖
這照著步驟就通了..YA!!
https://support.microsoft.com/zh-tw/help/214269/how-to-use-the-histogram-tool-in-excel
https://support.microsoft.com/zh-tw/help/214269/how-to-use-the-histogram-tool-in-excel
2017年2月1日 星期三
[Raspberry] 小知識
不熟,加減學,不錯的文章......
樹莓派 Raspberry Pi 在命令列設定 WiFi 無線網路的指令教學
https://blog.gtwang.org/iot/raspberry-pi/raspberry-pi-wireless-configuration-command-tutorial/
在 Raspberry Pi 中使用 USB 網路攝影機(Webcam)照相
http://blogger.gtwang.org/2015/01/raspberry-pi-usb-webcam.html
樹莓派 Raspberry Pi 與 USB 網路攝影機,自製縮時攝影設備教學
https://blog.gtwang.org/iot/raspberry-pi/raspberry-pi-time-lapse-using-motion-and-webcam/
樹莓派 Raspberry Pi 設定無線網路 WiFi AP,打造無線 IP 分享器
https://blog.gtwang.org/iot/setup-raspberry-pi-as-wireless-access-point/
Raspberry Pi 的實作 - 自動配置 IP 位址資訊的 DHCP Server
http://blog.itist.tw/2015/01/raspberry-pi-dhcp-server.html
樹莓派 Raspberry Pi 設定無線網路 WiFi AP,打造無線 IP 分享器
https://blog.gtwang.org/iot/setup-raspberry-pi-as-wireless-access-point/2/
樹莓派 Raspberry Pi 在命令列設定 WiFi 無線網路的指令教學
https://blog.gtwang.org/iot/raspberry-pi/raspberry-pi-wireless-configuration-command-tutorial/
在 Raspberry Pi 中使用 USB 網路攝影機(Webcam)照相
http://blogger.gtwang.org/2015/01/raspberry-pi-usb-webcam.html
樹莓派 Raspberry Pi 與 USB 網路攝影機,自製縮時攝影設備教學
https://blog.gtwang.org/iot/raspberry-pi/raspberry-pi-time-lapse-using-motion-and-webcam/
樹莓派 Raspberry Pi 設定無線網路 WiFi AP,打造無線 IP 分享器
https://blog.gtwang.org/iot/setup-raspberry-pi-as-wireless-access-point/
Raspberry Pi 的實作 - 自動配置 IP 位址資訊的 DHCP Server
http://blog.itist.tw/2015/01/raspberry-pi-dhcp-server.html
樹莓派 Raspberry Pi 設定無線網路 WiFi AP,打造無線 IP 分享器
https://blog.gtwang.org/iot/setup-raspberry-pi-as-wireless-access-point/2/
2017年1月4日 星期三
[C#} 如何 read/write app.config
對專案按滑鼠右鍵,選擇 Properties,選擇左邊的Tab的Settings,加入所需參數
選表單的Porject/Add Reference,加入 System.Configuration
範例:
using System.Configuration;
namespace AppConfigExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int num = Properties.Settings.Default.Num;
textBox1.Text = Convert.ToString(num);
}
private void button1_Click(object sender, EventArgs e)
{
int num = Convert.ToInt32(textBox1.Text);
Properties.Settings.Default.Num=num;
Properties.Settings.Default.Save();
}
}
}
選表單的Porject/Add Reference,加入 System.Configuration
範例:
using System.Configuration;
namespace AppConfigExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int num = Properties.Settings.Default.Num;
textBox1.Text = Convert.ToString(num);
}
private void button1_Click(object sender, EventArgs e)
{
int num = Convert.ToInt32(textBox1.Text);
Properties.Settings.Default.Num=num;
Properties.Settings.Default.Save();
}
}
}
不錯的文章:
http://wlchang1108.blogspot.tw/2016/08/visual-studioproertiessettingssettings.html
https://dotblogs.com.tw/yc421206/2010/08/08/17076
2016年11月15日 星期二
[C#]開機自動執行
using Microsoft.Win32;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string app_name= "WindowsFormsApplication1";
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
if (rk2.GetValue(app_name) != null)
{
rk2.DeleteValue(app_name, false);
}
rk2.SetValue(app_name, path);
rk2.Close();
rk.Close();
}
catch(Exception ex)
{
MessageBox.Show("fail\n");
}
}
}
}
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string app_name= "WindowsFormsApplication1";
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
if (rk2.GetValue(app_name) != null)
{
rk2.DeleteValue(app_name, false);
}
rk2.SetValue(app_name, path);
rk2.Close();
rk.Close();
}
catch(Exception ex)
{
MessageBox.Show("fail\n");
}
}
}
}
2016年11月14日 星期一
[C++]Interprocess Communications (MailSlot)
#include "stdafx.h"
#include <Windows.h>
#include <strsafe.h>
#include <stdio.h>
HANDLE hSlot;
LPTSTR Slot = TEXT("\\\\.\\mailslot\\test_mailslot");
BOOL ReadSlot()
{
DWORD cbMessage, cMessage, cbRead;
BOOL fResult;
LPTSTR lpszBuffer;
TCHAR achID[80];
DWORD cAllMessages;
HANDLE hEvent;
OVERLAPPED ov;
cbMessage = cMessage = cbRead = 0;
hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot"));
if (NULL == hEvent)
return FALSE;
ov.Offset = 0;
ov.OffsetHigh = 0;
ov.hEvent = hEvent;
fResult = GetMailslotInfo(hSlot, (LPDWORD)NULL, &cbMessage, &cMessage, (LPDWORD)NULL);
if (!fResult)
{
printf("Get MailSlot failed %d.\n", GetLastError());
return FALSE;
}
if (cbMessage == MAILSLOT_NO_MESSAGE)
{
printf("Waiting for a message\n");
return TRUE;
}
cAllMessages = cMessage;
while (cMessage != 0)
{
StringCchPrintf((LPTSTR)achID, 80, TEXT("\nMessage #%d of %d\n"), cAllMessages - cMessage + 1, cAllMessages);
lpszBuffer = (LPTSTR)GlobalAlloc(GPTR, lstrlen((LPTSTR)achID)*sizeof(TCHAR) + cbMessage);
if (NULL == lpszBuffer) return FALSE;
lpszBuffer[0] = '\0';
fResult = ReadFile(hSlot, lpszBuffer, cbMessage, &cbRead, &ov);
if (!fResult)
{
printf("Read Fail %d\n", GetLastError());
GlobalFree((HGLOBAL)lpszBuffer);
return FALSE;
}
StringCbCat(lpszBuffer, lstrlen((LPTSTR)achID)*sizeof(TCHAR) + cbMessage, (LPTSTR)achID);
_tprintf(TEXT("Contents of the mail:%s\n"), lpszBuffer);
GlobalFree((HGLOBAL)lpszBuffer);
fResult = GetMailslotInfo(hSlot, (LPDWORD)NULL, &cbMessage, &cMessage, (LPDWORD)NULL);
if (!fResult)
{
printf("Get Mail Slot Info %d\n", GetLastError());
}
}
CloseHandle(hEvent);
return TRUE;
}
int main()
{
hSlot = CreateMailslot(Slot, 0, MAILSLOT_WAIT_FOREVER, (LPSECURITY_ATTRIBUTES) NULL);
if (hSlot == INVALID_HANDLE_VALUE)
{
printf("Create Mail Slot Failed:%d \n", GetLastError());
return 0;
}
else printf("Mail slot create ok!!\n");
while (TRUE)
{
ReadSlot();
Sleep(3000);
}
}
=============================================================================================
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
LPTSTR SlotName = TEXT("\\\\.\\mailslot\\test_mailslot");
int main()
{
HANDLE hfile;
hfile = CreateFile(SlotName, GENERIC_WRITE, FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
if (hfile == INVALID_HANDLE_VALUE)
{
printf("CreateFile failed with %d\n", GetLastError());
return FALSE;
}
BOOL fResult;
DWORD cbWritten;
fResult = WriteFile(hfile, SlotName, (DWORD)(lstrlen(SlotName) + 1)*sizeof(TCHAR), &cbWritten, (LPOVERLAPPED)NULL);
if (!fResult)
{
printf("Write file failed:%d \n", GetLastError());
return FALSE;
}
printf("Slot written to successfully \n");
CloseHandle(hfile);
for (int h = 0; h < 10000; h++) Sleep(2000);
return 0;
}
執行結果:
#include <Windows.h>
#include <strsafe.h>
#include <stdio.h>
HANDLE hSlot;
LPTSTR Slot = TEXT("\\\\.\\mailslot\\test_mailslot");
BOOL ReadSlot()
{
DWORD cbMessage, cMessage, cbRead;
BOOL fResult;
LPTSTR lpszBuffer;
TCHAR achID[80];
DWORD cAllMessages;
HANDLE hEvent;
OVERLAPPED ov;
cbMessage = cMessage = cbRead = 0;
hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot"));
if (NULL == hEvent)
return FALSE;
ov.Offset = 0;
ov.OffsetHigh = 0;
ov.hEvent = hEvent;
fResult = GetMailslotInfo(hSlot, (LPDWORD)NULL, &cbMessage, &cMessage, (LPDWORD)NULL);
if (!fResult)
{
printf("Get MailSlot failed %d.\n", GetLastError());
return FALSE;
}
if (cbMessage == MAILSLOT_NO_MESSAGE)
{
printf("Waiting for a message\n");
return TRUE;
}
cAllMessages = cMessage;
while (cMessage != 0)
{
StringCchPrintf((LPTSTR)achID, 80, TEXT("\nMessage #%d of %d\n"), cAllMessages - cMessage + 1, cAllMessages);
lpszBuffer = (LPTSTR)GlobalAlloc(GPTR, lstrlen((LPTSTR)achID)*sizeof(TCHAR) + cbMessage);
if (NULL == lpszBuffer) return FALSE;
lpszBuffer[0] = '\0';
fResult = ReadFile(hSlot, lpszBuffer, cbMessage, &cbRead, &ov);
if (!fResult)
{
printf("Read Fail %d\n", GetLastError());
GlobalFree((HGLOBAL)lpszBuffer);
return FALSE;
}
StringCbCat(lpszBuffer, lstrlen((LPTSTR)achID)*sizeof(TCHAR) + cbMessage, (LPTSTR)achID);
_tprintf(TEXT("Contents of the mail:%s\n"), lpszBuffer);
GlobalFree((HGLOBAL)lpszBuffer);
fResult = GetMailslotInfo(hSlot, (LPDWORD)NULL, &cbMessage, &cMessage, (LPDWORD)NULL);
if (!fResult)
{
printf("Get Mail Slot Info %d\n", GetLastError());
}
}
CloseHandle(hEvent);
return TRUE;
}
int main()
{
hSlot = CreateMailslot(Slot, 0, MAILSLOT_WAIT_FOREVER, (LPSECURITY_ATTRIBUTES) NULL);
if (hSlot == INVALID_HANDLE_VALUE)
{
printf("Create Mail Slot Failed:%d \n", GetLastError());
return 0;
}
else printf("Mail slot create ok!!\n");
while (TRUE)
{
ReadSlot();
Sleep(3000);
}
}
=============================================================================================
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
LPTSTR SlotName = TEXT("\\\\.\\mailslot\\test_mailslot");
int main()
{
HANDLE hfile;
hfile = CreateFile(SlotName, GENERIC_WRITE, FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
if (hfile == INVALID_HANDLE_VALUE)
{
printf("CreateFile failed with %d\n", GetLastError());
return FALSE;
}
BOOL fResult;
DWORD cbWritten;
fResult = WriteFile(hfile, SlotName, (DWORD)(lstrlen(SlotName) + 1)*sizeof(TCHAR), &cbWritten, (LPOVERLAPPED)NULL);
if (!fResult)
{
printf("Write file failed:%d \n", GetLastError());
return FALSE;
}
printf("Slot written to successfully \n");
CloseHandle(hfile);
for (int h = 0; h < 10000; h++) Sleep(2000);
return 0;
}
執行結果:
訂閱:
文章 (Atom)
