天天看點

雜記 (7) —— shell, gdb, vim, gcc, mingw32

diff的結果顯示解釋

a - add
c - change
d - delete      

如:

$ diff file1 file2

1c1
< 時間:0.29000000
---
> 時間:0.42000000      

代表第一行不同

1,2d0
< 時間:0.29000000      

代表第一個檔案比第二個檔案多第一行​

​時間:0.29000000​

​​和第二行​

​after sorted :​

​。

0a1
> hello      

代表第一個檔案比第二個檔案少第一行hello。

檢視磁盤空間

edemon@ubuntu1:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            491M     0  491M   0% /dev
tmpfs           101M  5.2M   95M   6% /run
/dev/sda5        20G  6.3G   13G  34% /
tmpfs           501M  156K  501M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           501M     0  501M   0% /sys/fs/cgroup
cgmfs           100K     0  100K   0% /run/cgmanager/fs
tmpfs           101M   56K  100M   1% /run/user/1000
/dev/sr0        1.2G  1.2G     0 100% /media/edemon/Ubuntu 15.04      

檢視CentOS上mysql的版本

$ rpm -qi mysql-server
Name        : mysql-server                 Relocations: (not relocatable)
Version     : 5.1.73                            Vendor: CentOS
Release     : 7.el6                         Build Date: Wed 11 May 2016 02:05:53 PM CST
Install Date: Wed 07 Dec 2016 08:44:50 AM CST      Build Host: worker1.bsys.centos.org
Group       : Applications/Databases        Source RPM: mysql-5.1.73-7.el6.src.rpm
Size        : 25688915                         License: GPLv2 with exceptions
Signature   : RSA/SHA1, Thu 12 May 2016 06:46:25 PM CST, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : http://www.mysql.com
Summary     : The MySQL server and related files
Description :
MySQL is a multi-user, multi-threaded SQL database server. MySQL is a
client/server implementation consisting of a server daemon (mysqld)
and many different client programs and libraries. This package contains
the MySQL server and some accompanying files and directories.      

産看檔案系統類型

root@ubuntu1:/etc/init.d# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            491M     0  491M   0% /dev
tmpfs           101M  5.2M   95M   6% /run
/dev/sda5        20G  6.7G   12G  36% /
tmpfs           501M  156K  501M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           501M     0  501M   0% /sys/fs/cgroup
cgmfs           100K     0  100K   0% /run/cgmanager/fs
tmpfs           101M   48K  100M   1% /run/user/1000
/dev/sr0        1.2G  1.2G     0 100% /media/edemon/Ubuntu 15.04 i386

root@ubuntu1:/etc/init.d# file -s /dev/sda5
/dev/sda5: Linux rev 1.0 ext2 filesystem data (mounted or unclean), UUID=acbb6d51-cbc0-40cc-8341-d4b0a4471b44 (large files)      

Can’t connect to local MySQL server through socket

抱着折騰的心思,我關閉了mysql服務端。

root@ubuntu1:~# mysqladmin -u root -p shutdown      

然後嘗試重新開啟,出現下列錯誤:

root@ubuntu1:~# mysqld_safe &
[1] 6366
root@ubuntu1:~# 161211 10:20:13 mysqld_safe Can't log to error log and syslog at the same time.  Remove all --log-error configuration options for --syslog to take effect.
161211 10:20:13 mysqld_safe Logging to '/var/log/mysql/error.log'.
161211 10:20:13      

根據最後一句話的提示,我殺死mysqld程序。但是随後新的mysqld又出現了。查閱後,我将/etc/init.d/mysql删除了(準确的說是誤删,mv寫成了rm,word天 -_-||)

神奇的是我又能正常登陸mysql了。這正常嗎。。。

我重新來了一次,發現是這樣的:

mysqld确實正常啟動了:

root@ubuntu1:/etc/init.d# mysqld_safe &
[1] 7276
root@ubuntu1:/etc/init.d# 161211 11:09:28 mysqld_safe Can't log to error log and syslog at the same time.  Remove all --log-error configuration options for --syslog to take effect.
161211 11:09:28 mysqld_safe Logging to '/var/log/mysql/error.log'.
161211 11:09:28 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
161211 11:09:30      

登陸時出現問題:

edemon@ubuntu1:~$ mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)      

我查找sock檔案,它并不存在。

重新開機mysql:

edemon@ubuntu1:~$ service mysql restart      

OK:

root@ubuntu1:/home/edemon# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3      

不過我在CentOS上使用同樣的指令時他不認識mysql,我搜尋了一下mysqld,然後重新開機mysql服務,最後正常了。​

​/etc/rc.d/init.d/mysqld restart​

bc進行浮點運算

man線上手冊中有這樣的話:

NUMBERS
 45        The most basic element in bc is the number.  Numbers are arbitrary precision numbers.  This precision is both in the integer part and the
 46        fractional part.  All numbers are represented internally in decimal and all computation is done  in  decimal.   (This  version  truncates
 47        results  from  divide  and multiply operations.)  There are two attributes of numbers, the length and the scale.  The length is the total
 48        number of significant decimal digits in a number and the scale is the total number of decimal digits after the decimal point.  For  exam‐
 49        ple:
 50            .000001 has a length of 6 and scale of 6.
 51            1935.000 has a length of 7 and a scale of 3.      

是以我們可以通過改變scale來控制輸出浮點數的精度。

比如:

edemon@ubuntu1:~$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
2/4
0
scale=2
2/4
.50      

檢視cpu、處理器等硬體資訊

​cat /proc/cpuinfo​

gdb臨時改變變量的值

假設改變浮點數value1的值:

(gdb) set var value1=1      

負數的增長

-1在計算機中的表示:

32整數 —— ​​

​11111111111111111111111111111111​

​​

僅從負數的角度來看,負數在計算機中的表示也有一定的規律:

(gdb) set var $eax=-1
(gdb) p/t $eax
$3 = 11111111111111111111111111111111
(gdb) set var $eax=-2
(gdb) p/t $eax
$4 = 11111111111111111111111111111110
(gdb) set var $eax=-3
(gdb) p/t $eax
$5 = 11111111111111111111111111111101
(gdb) set var $eax=-4
(gdb) p/t $eax
$6 = 11111111111111111111111111111100
(gdb) set var $eax=-5
(gdb) p/t $eax
$7 = 11111111111111111111111111111011
(gdb) set var $eax=-6
(gdb) p/t $eax
$8 = 11111111111111111111111111111010      

Binary file (standard input) matches

grep提取特定資訊出錯,加上選項-a即可。

​​

​read_command |grep -a condition​

​​

專用于處理二進制檔案的情況:

​​

​Process a binary file as if it were text; this is equivalent to the --binary-files=text option​

查gdb調試運作到哪一行

​(gdb) bt​

​檢視堆棧資訊,同時也能檢視行數資訊。

vim字型設定

​ctrl \+ 0​

​​ 正常字型

​​

​ctrl \+ shft \+ +​

​​ 字型加大

​​

​ctrl \+ -​

​字型變小

at計劃任務

linux at指令可以用于在特定的時間執行設定的任務,如:

at 14:40
at> ls -l > text
at> <EOT>
job 8 at 2017-01-18 14:40      

檢視at任務清單:​

​atq​

​​;删除at任務: ​

​atrm Job_number​

run level

Linux預設的運作級别是

[root@CentOS Desktop]# runlevel
N 5      

檢視各個運作級别的含義:

[root@CentOS Desktop]# cat /etc/inittab
...
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
#       

​init 0​

​可關機。

crontab

crontab的安裝:​

​yum install -y vixie-cron​

​​

​​

​crontab -e​

​​編輯crontab檔案,不存在則自動建立。

crontab前5列是時間和日期。

9 # .---------------- minute (0 - 59)
 10 # |  .------------- hour (0 - 23)
 11 # |  |  .---------- day of month (1 - 31)
 12 # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
 13 # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
 14 # |  |  |  |  |
 15 # *  *  *  *  * user-name command to      

開啟crond服務:

[root@CentOS Desktop]# /etc/rc.d/init.d/crond restart
Starting crond:                                            [  OK  ]
[root@CentOS Desktop]# ps -ef |grep crond
root      3636     1 15 15:41 ?        00:00:01 crond
root      3639  2872  0 15:41 pts/0    00:00:00 grep      

在crontab檔案中編輯任務:​

​vim /etc/crontab​

​​ 末尾增加任務:​

​19 16 * * * date > /home/edemon/workspace/text​

​​

最後檢視結果:

[edemon@CentOS workspace]$ cat text
Wed Jan 18 16:19:01 CST 2017      

使用​

​crontab -e​

​來制定任務卻一直未能成功,迷惑啊。

檢視gcc的搜尋路徑

先看看gcc幫助:

shell> gcc --help
Usage: gcc [options] file...
Options:
  -pass-exit-codes         Exit  --help                   Display  --target-help            Display  --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]
                           Display specific types of command line options
  (Use '-v --help' to display command line options of sub-processes)
  --version                Display  -dumpspecs               Display all of the built in  -dumpversion             Display  -dumpmachine             Display the compiler's target processor
  -print-search-dirs       Display the directories in the compiler's search path
  -print-libgcc-file-name  Display the name of the compiler's companion library
  -print-file-name=<lib>   Display  -print-prog-name=<prog>  Display  -print-multiarch         Display the target's normalized GNU triplet, used as
                           a component in the library path
  -print-multi-directory   Display the root directory for  -print-multi-lib         Display the mapping between command line options and                           multiple library search directories
  -print-multi-os-directory Display the relative path to OS  -print-sysroot           Display  -print-sysroot-headers-suffix Display  -Wa,<options>            Pass  -Wp,<options>            Pass  -Wl,<options>            Pass  -Xassembler <arg>        Pass  -Xpreprocessor <arg>     Pass  -Xlinker <arg>           Pass  -save-temps              Do not  -save-temps=<arg>        Do not  -no-canonical-prefixes   Do not canonicalize paths when                           prefixes to other gcc components
  -pipe                    Use  -time                    Time  -specs=<file>            Override built-in  -std=<standard>          Assume that the input sources are for  --sysroot=<directory>    Use <directory> as the root directory for                           and libraries
  -B <directory>           Add <directory> to the compiler's search paths
  -v                       Display  -###                     Like -v but options quoted and commands not executed
  -E                       Preprocess only; do not compile, assemble or  -S                       Compile only; do not assemble or  -c                       Compile and assemble, but do not  -o <file>                Place  -pie                     Create  -shared                  Create  -x <language>            Specify      

根據提示查找搜尋:

shell> gcc -print-search-dirs
install: /usr/lib/gcc/i686-linux-gnu/4.9/
programs: =/usr/lib/gcc/i686-linux-gnu/4.9/:/usr/lib/gcc/i686-linux-gnu/4.9/:/usr/lib/gcc/i686-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.9/:/usr/lib/gcc/i686-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.9/../../../../i686-linux-gnu/bin/i686-linux-gnu/4.9/:/usr/lib/gcc/i686-linux-gnu/4.9/../../../../i686-linux-gnu/bin/i386-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.9/../../../../i686-linux-gnu/bin/
libraries: =/usr/lib/gcc/i686-linux-gnu/4.9/:/usr/lib/gcc/i686-linux-gnu/4.9/../../../../i686-linux-gnu/lib/i686-linux-gnu/4.9/:/usr/lib/gcc/i686-linux-gnu/4.9/../../../../i686-linux-gnu/lib/i386-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.9/../../../../i686-linux-gnu/lib/../lib/:/usr/lib/gcc/i686-linux-gnu/4.9/../../../i686-linux-gnu/4.9/:/usr/lib/gcc/i686-linux-gnu/4.9/../../../i386-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.9/../../../../lib/:/lib/i686-linux-gnu/4.9/:/lib/i386-linux-gnu/:/lib/../lib/:/usr/lib/i686-linux-gnu/4.9/:/usr/lib/i386-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/i686-linux-gnu/4.9/../../../../i686-linux-gnu/lib/:/usr/lib/gcc/i686-linux-gnu/4.9/../../../:/lib/:/usr/lib/

#for c:
shell> `gcc  -print-prog-name=cc1` -v
ignoring nonexistent directory "/usr/lib/gcc/i686-linux-gnu/4.9/../../../../i686-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i686-linux-gnu/4.9/include
 /usr/local/include
 /usr/lib/gcc/i686-linux-gnu/4.9/include-fixed
 /usr/include
End of search list.

#for c++
shell> `gcc  -print-prog-name=cc1plus` -v
ignoring nonexistent directory "/usr/lib/gcc/i686-linux-gnu/4.9/../../../../i686-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.9
 /usr/include/i386-linux-gnu/c++/4.9
 /usr/include/c++/4.9/backward
 /usr/lib/gcc/i686-linux-gnu/4.9/include
 /usr/local/include
 /usr/lib/gcc/i686-linux-gnu/4.9/include-fixed
 /usr/include
End of search list.
^C      

mingw32頭檔案和庫檔案

查找mingw32的搜尋路徑是一樣的:

shell> i686-w64-mingw32-gcc --help
Usage: i686-w64-mingw32-gcc [options] file...
Options:
  -pass-exit-codes         Exit with highest error code from a phase
  --help                   Display this information
  --target-help            Display target specific command line options
  --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]
                           Display specific types of command line options
  (Use '-v --help' to display command line options of sub-processes)
  --version                Display compiler version information
  -dumpspecs               Display all of the built in spec strings
  -dumpversion             Display the version of the compiler
  -dumpmachine             Display the compiler's target processor
  -print-search-dirs       Display the directories in the compiler's search path
  -print-libgcc-file-name  Display the name of the compiler's companion library
  -print-file-name=<lib>   Display the full path to library <lib>
  -print-prog-name=<prog>  Display the full path to compiler component <prog>
  -print-multiarch         Display the target's normalized GNU triplet, used as
                           a component in the library path
  -print-multi-directory   Display the root directory for versions of libgcc
  -print-multi-lib         Display the mapping between command line options and
                           multiple library search directories
  -print-multi-os-directory Display the relative path to OS libraries
  -print-sysroot           Display the target libraries directory
  -print-sysroot-headers-suffix Display the sysroot suffix used to find headers
  -Wa,<options>            Pass comma-separated <options> on to the assembler
  -Wp,<options>            Pass comma-separated <options> on to the preprocessor
  -Wl,<options>            Pass comma-separated <options> on to the linker
  -Xassembler <arg>        Pass <arg> on to the assembler
  -Xpreprocessor <arg>     Pass <arg> on to the preprocessor
  -Xlinker <arg>           Pass <arg> on to the linker
  -save-temps              Do not delete intermediate files
  -save-temps=<arg>        Do not delete intermediate files
  -no-canonical-prefixes   Do not canonicalize paths when building relative
                           prefixes to other gcc components
  -pipe                    Use pipes rather than intermediate files
  -time                    Time the execution of each subprocess
  -specs=<file>            Override built-in specs with the contents of <file>
  -std=<standard>          Assume that the input sources are for <standard>
  --sysroot=<directory>    Use <directory> as the root directory for headers
                           and libraries
  -B <directory>           Add <directory> to the compiler's search paths
  -v                       Display the programs invoked by the compiler
  -###                     Like -v but options quoted and commands not executed
  -E                       Preprocess only; do not compile, assemble or link
  -S                       Compile only; do not assemble or link
  -c                       Compile and assemble, but do not link
  -o <file>                Place the output into <file>
  -pie                     Create a position independent executable
  -shared                  Create a shared library
  -x <language>            Specify the language of the following input files
                           Permissible languages include: c c++ assembler none
                           'none' means revert to the default behavior of
                           guessing the language based on the file's extension

Options starting with -g, -f, -m, -O, -W, or --param are automatically
 passed on to the various sub-processes invoked by i686-w64-mingw32-gcc.  In order to pass
 other options on to these processes the -W<letter> options must be used.

For bug reporting instructions, please see:
<http://gcc.gnu.org/bugs.html>.      

查找c的頭檔案搜尋路徑:

shell> `i686-w64-mingw32-gcc -print-prog-name=cc1` -v
ignoring nonexistent directory "/usr/lib/gcc/i686-w64-mingw32/4.9-win32/../../../../i686-w64-mingw32/sys-include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i686-w64-mingw32/4.9-win32/include
 /usr/lib/gcc/i686-w64-mingw32/4.9-win32/include-fixed
 /usr/lib/gcc/i686-w64-mingw32/4.9-win32/../../../../i686-w64-mingw32/include
End of search list.      

檢視庫檔案搜尋目錄:

[edemon@CentOS ~]$  i686-w64-mingw32-gcc   -print-search-dirs 
install: /usr/lib/gcc/i686-w64-mingw32/4.9.2/
programs: =/usr/libexec/gcc/i686-w64-mingw32/4.9.2/:/usr/libexec/gcc/i686-w64-mingw32/4.9.2/:/usr/libexec/gcc/i686-w64-mingw32/:/usr/lib/gcc/i686-w64-mingw32/4.9.2/:/usr/lib/gcc/i686-w64-mingw32/:/usr/lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/i686-w64-mingw32/4.9.2/:/usr/lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/
libraries: =/usr/lib/gcc/i686-w64-mingw32/4.9.2/:/usr/lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/lib/i686-w64-mingw32/4.9.2/:/usr/lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/lib/../lib/:/usr/i686-w64-mingw32/sys-root/mingw/lib/i686-w64-mingw32/4.9.2/:/usr/i686-w64-mingw32/sys-root/mingw/lib/../lib/:/usr/lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/lib/:/usr/i686-w64-mingw32/sys-root/mingw/lib/      

優先搜尋多個頭檔案庫

i686-w64-mingw32-gcc sdl_back.c -lSDL -I/usr/include/ -I/usr/include/i386-linux-gnu/gnu/      

mingw32在linux平台生成windows可執行檔案

#include <stdio.h>
int main(){
    printf("hello\n");
    return 0;
}      
root@ubuntu1:/home/edemon/workspace# i686-w64-mingw32-gcc -S -o win.s win.c 
root@ubuntu1:/home/edemon/workspace# i686-w64-mingw32-gcc -o win.exe win.c 
root@ubuntu1:/home/edemon/workspace# file win.exe
win.exe: PE32 executable (console) Intel 80386, for MS Windows
root@ubuntu1:/home/edemon/workspace# file win.s
win.s: assembler source, ASCII text
root@ubuntu1:/home/edemon/workspace# cat win.s
    .file   "win.c"
    .def    ___main;    .scl    2;  .type   32; .endef
    .section .rdata,"dr"
LC0:
    .ascii "hello\0"
    .text
    .globl  _main
    .def    _main;  .scl    2;  .type   32; .endef
_main:
    pushl   %ebp
    movl    %esp, %ebp
    andl    $-16, %esp
    subl    $16, %esp
    call    ___main
    movl    $LC0, (%esp)
    call    _puts
    movl    $0, %eax
    leave
    ret
    .ident  "GCC: (GNU) 4.9.2"
    .def    _puts;  .scl    2;  .type   32; .endef      

繼續閱讀