advertisement

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

2021年9月14日

What is /dev/null 2>&1 linux file descriptor 0 1 2 for stdin, stdout and stderr In the shell, what does " 2>&1 " mean?

 

         The input stream is referred to as "standard input"; the output
       stream is referred to as "standard output"; and the error stream
       is referred to as "standard error".  These terms are abbreviated
       to form the symbols used to refer to these files, namely stdin,
       stdout, and stderr.
         On program startup, the integer file descriptors associated with
       the streams stdin, stdout, and stderr are 0, 1, and 2,
       respectively.  The preprocessor symbols STDIN_FILENO,
       STDOUT_FILENO, and STDERR_FILENO are defined with these values in
       <unistd.h>.


There are also symbolic constants defined in unistd.h for the file descriptors belonging to the standard streams stdinstdout, and stderr; see Standard Streams.

STDIN_FILENO

This macro has value 0, which is the file descriptor for standard input.

STDOUT_FILENO

This macro has value 1, which is the file descriptor for standard output.

STDERR_FILENO

This macro has value 2, which is the file descriptor for standard error output.


File descriptor 1 is the standard output (stdout).
File descriptor 2 is the standard error (stderr).

Here is one way to remember this construct (although it is not entirely accurate): at first, 2>1 may look like a good way to redirect stderr to stdout. However, it will actually be interpreted as "redirect stderr to a file named 1". & indicates that what follows and precedes is a file descriptor and not a filename. So the construct becomes: 2>&1.

Consider >& as redirect merger operator.


Ref:

https://www.gnu.org/software/libc/manual/html_node/Descriptors-and-Streams.html

https://man7.org/linux/man-pages/man3/stdout.3.html

https://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean?noredirect=1&lq=1

Double pipe Double logical and Control operators

 

expression )

Returns the value of expression. This may be used to override the normal precedence of operators.

expression

True if expression is false.

expression1 && expression2

True if both expression1 and expression2 are true.

expression1 || expression2

True if either expression1 or expression2 is true.

The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.


For example:

Code:
$ ls this_file_does_not_exist.txt || echo KO
ls: cannot access this_file_does_not_exist.txt: No such file or directory
KO

$ ls this_file_exist.txt && echo OK
this_file_exist.txt
OK


Ref:

https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Command-Grouping

https://pongsona.wordpress.com/2011/12/03/what-does-double-pipe-mean/


What does @: (at symbol colon) mean in a Makefile?

 https://stackoverflow.com/questions/8610799/what-does-at-symbol-colon-mean-in-a-makefile

https://unix.stackexchange.com/questions/192241/file-function-in-makefile-takes-args-prefixed-by-symbol



2021年4月20日

linux下 串列埠 工具 串口 序列埠 軟體 linux putty alternative for serial

 請參考此篇文章 Use Serial Port To Remote Control


使用cu command

cu -l /dev/device -s baud-rate-speed

# cu -l /dev/ttyS0 -s 57600

若出現

cu: open (/dev/ttyS0): Permission denied

cu: /dev/ttyS0: Line in use

請打下列指令確認權限

# ls -la /dev/ttyUSB0

# chmod 666 /dev/ttyUSB0


ref:https://superuser.com/questions/794309/linux-tells-me-a-serial-port-is-in-use-but-it-isnt/1155172

https://www.cyberciti.biz/hardware/5-linux-unix-commands-for-connecting-to-the-serial-console/

2021年4月6日

Debian Buster 10 root $PATH in bash does not include /sbin and /usr/sbin

 Debian 10 切換root超級使用者權限,超級使用者/管理者指令找不到,下echo $PATH發現環境變數與一般使用者一樣。

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

$ su
密碼:

# echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games


根據Debian官方說明 Debian 10 su 指令是使用了util-linux套件,其PATH使用預設值且不會警告,若要切換至超級使用者與環境變數,須下達 su - 或 su -l 或 su --login ,三者亦同。

$ su -
密碼:
root@debian10:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin


Reference:

https://wiki.debian.org/NewInBuster

https://unix.stackexchange.com/questions/460478/debian-su-and-su-path-differences

 

#ifconfig, #debian 10, #debian buster, #chpasswd, #fdisk, ,#useradd, #lsmod, #command not found, #指令找不到, #命令找不到

 


2019年8月9日

Linux 學習

Other users' home directories
Bash will expand a lone ~ to point to your home directory, but you can also use it to point to
other users' home directories. For example, if we wanted to refer to a file called fredsfile.txt in
Fred's home directory, we could type:
$ ./myprog ~fred/fredsfile.txt

my home directories
example: $ ./myprog ~/myfile.txt

Looking at directories
Sometimes, you'll want to look at a directory, rather than inside it. For these situations, you
can specify the -d option, which will tell ls to look at any directories that it would normally
look inside:
$ ls -dl /usr /usr/bin /usr/X11R6/bin ../share
drwxr-xr-x 4 root root 96 Dec 18 18:17 ../share
drwxr-xr-x 17 root root 576 Dec 24 09:03 /usr
drwxr-xr-x 2 root root 3192 Dec 26 12:52 /usr/X11R6/bin
drwxr-xr-x 2 root root 14576 Dec 27 08:56 /usr/bin

$ ls -dl /usr/local
drwxr-xr-x 8 root root 240 Dec 22 20:57 /usr/local
If we take a look at the second column from the left, we see that the directory /usr/local
(inode 5120) is referenced eight times. On my system, here are the various paths that
reference this inode:
/usr/local
/usr/local/.
/usr/local/bin/..
/usr/local/games/..
/usr/local/lib/..
/usr/local/sbin/..
/usr/local/share/..
/usr/local/src/..

mkdir -p
However, mkdir has a handy -p option that tells mkdir to create any missing parent
directories, as you can see here:
$ mkdir -p easy/as/pie

After typing this command, myfile.txt will be moved to /home/drobbins/myfile.txt. And if
/home/drobbins is on a different filesystem than /var/tmp, the mv command will handle the
copying of myfile.txt to the new filesystem and erasing it from the old filesystem. As you might guess, when myfile.txt is moved between filesystems, the myfile.txt at the new location
will have a new inode number. This is because every filesystem has its own independent set
of inode numbers.



A given inode can have any number of hard links, and
the inode will persist on the filesystem until the all the hard links disappear. When the last
hard link disappears and no program is holding the file open, Linux will delete the file
automatically.
you can only make hard links to files,not directories.

The second limitation
of hard links is that they can't span filesystems. This means that you can't create a link from
/usr/bin/bash to /bin/bash if your / and /usr directories exist on separate filesystems.


Note that double quotes will work similarly to single quotes, but will still allow bash to do
some limited expansion. Therefore, single quotes are your best bet when you are truly
interested in passing literal text to a command.


wildcards=globbing

https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html
https://www.linuxjournal.com/content/bash-extended-globbing.
http://www.globtester.com/ globbing debuger

If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched.
wayne.su@rd1-4:~/configdec$ ls
myfile1  myfile2  myfile3  myfile4
wayne.su@rd1-4:~/configdec$ ls myfile[!2]
myfile1  myfile3  myfile4
wayne.su@rd1-4:~/configdec$ ls myfile[^2]

myfile1  myfile3  myfile4

regular expression


grep regex operatorMeaningExample
.Matches any single character.grep '.' file
grep 'foo.' input
?The preceding item(k) is optional and will be matched zero or at most, once.grep 'vivek?' /etc/passwd
*The preceding item(k) will be matched zero or more times.grep 'vivek*' /etc/passwd
+The preceding item will be matched one or more times.ls /var/log/ | grep -E "^[a-z]+\.log."
{N}The preceding item is matched exactly N times.egrep '[0-9]{2} input
{N,}The preceding item is matched N or more times.egrep '[0-9]{2,} input
{N,M}The preceding item is matched at least N times, but not more than M times.egrep '[0-9]{2,4} input
Represents the range if it’s not first or last in a list or the ending point of a range in a list.grep ':/bin/[a-z]*' /etc/passwd
^Matches the empty string at the beginning of a line; also represents the characters not in the range of a list.grep '^vivek' /etc/passwd
grep '[^0-9]*' /etc/passwd
$Matches the empty string at the end of a line.grep '^$' /etc/passwd
\bMatches the empty string at the edge of a word.vivek '\bvivek' /etc/passwd
\BMatches the empty string provided it’s not at the edge of a word.grep '\B/bin/bash /etc/passwd
\<Match the empty string at the beginning of word.grep '\<="" kbd="" style="box-sizing: inherit;">
\>Match the empty string at the end of word.grep 'bash\>' /etc/passwd
grep '\' /etc/passwd



You can reverse the meaning of the square brackets by putting a ^ immediately after the [.
In this case, the brackets will match any character that is not listed inside the brackets.

Again, note that we use [^] with regular expressions
$ grep dev.hda[^12] /etc/fstab

/dev/hda3 / reiserfs noatime,ro 1 1

#/dev/hda4 /mnt/extra reiserfs noatime,rw 1 1

^ and $ can be combined to match an entire line. For example, the following regex will match
a line that starts with the # character and ends with the . character, with any number of other
characters in between:
$ grep '^#.*\.$' /etc/fstab
# /etc/fstab: static file system information



When bash starts it opens the three standard file descriptors: stdin (file descriptor 0), stdout (file descriptor 1), and stderr (file descriptor 2). You can open more file descriptors (such as 3, 4, 5, ...), and you can close them






~ # ls -l a
ls: a: No such file or directory
~ # ls -l a 2>abc
~ # cat abc
ls: a: No such file or directory
~ # ls -l a 1>abc
ls: a: No such file or directory
~ # cat abc
~ # ls -l a &>abc
~ # cat abc

ls: a: No such file or directory

https://catonmat.net/bash-one-liners-explained-part-three

bash exit code
http://www.tldp.org/LDP/abs/html/exitcodes.html


makefile .PHONY的作用

讓cc變成一個偽目標,即使沒有cc這個檔案也可以執行。





fakeroot vs sudo
Why not just use sudo?
First of all, you don't need root privileges to build software and you don't need root privileges to compress them. So if you don't need it, you'd have to really be a Windows user to even think of getting that permission. But sarcasm aside, you may not even have root password.

Besides, let's say you do have root permissions. And let's say you want to pretend that a file should have read access only to the root. So you sudo, actually change the file owner and permissions to root, you get out of root shell and try to package everything. You fail because now you cannot read the file anymore since you don't have root access. So you have to sudo and compress and build the package as root. Effectively, you have to do everything as root.

This is BadTM.

As a packager, you don't need root permissions and you shouldn't get it. When you install a package, you may need to install some file (A) as root and that's where you need root permissions. All fakeroot does is to make this possible. It lets the packager list A as owned by root for the archiver, so that when the package is decompressed by the user, the archiver demands root permission and creates A as owned by root.


2019年6月12日

安裝 Nvidia 顯示卡驅動

下載驅動
https://www.nvidia.com/object/unix.html

# bash -c "echo blacklist nouveau > /etc/modprobe.d/blacklist-nvidia-nouveau.conf"
# /sbin/shutdown -r now
# service lightdm stop
# chmod +x NVIDIA-Linux-x86_64-390.116.run
# ./NVIDIA-Linux-x86_64-390.116.run

-----Testing-----
$ nvidia-settings

2019年6月3日

Linux 方便簡單的截圖軟體 Imagemagick

安裝
# apt-get install imagemagick

執行
$ import sc1.png
$ import -window root image1.png

若出現
import: unable to open X server `' @ error/import.c/ImportImageCommand/366.
錯誤訊息

請重新開機即可

2019年6月2日

No common CD-ROM drive was detected 解決方式

安裝debian netinst  時出現 "No common CD-ROM drive was detected"

請使用https://rufus.ie/ rufus燒入軟體,將ISO燒製USB,並且選擇 DD Image mode


DD Image mode VS ISO Image mode

https://askubuntu.com/questions/896911/is-dd-image-disk-writing-permanent

2016年12月21日

Docker 安裝在 Windows 快速建置Linux Debian 環境

Host(PC)端安裝先前條件:

  1. Windows 7 64-bit OS 或 Windows 8 / Windows8.1
  2. Virtualization (虛擬化)已開啟
*如果你的作業系統是 Windows 10 請不要參照底下步驟
請參考這裡


OS作業系統請自行確認。

Virtualization 確認方式如下

Windows 8 / Windows8.1:
開啟工作管理員點選 CPU 如下圖

2016年12月11日

Compiler Wpasupplicant Source Code

# apt-get install openssl-dev libssl-dev libnl-3-dev libnl-genl-3-dev pkg-config

Then open the .config file:
vi .config
and uncomment the line:
CONFIG_LIBNL32=y
# make

https://w1.fi/wpa_supplicant/

2016年5月28日

Linux fglrx-driver 不支援 ATI AMD 顯示卡 移除方式

# aticonfig: no supported adapters detected

# cat /var/log/Xorg.0.log

$ sudo sh /usr/share/ati/fglrx-uninstall.sh
$ sudo apt-get remove --purge fglrx fglrx_* fglrx-amdcccle* fglrx-dev* xorg-driver-fglrx
$ sudo apt-get remove --purge xserver-xorg-video-ati xserver-xorg-video-radeon 
$ sudo apt-get install xserver-xorg-video-ati 
$ sudo apt-get install --reinstall libgl1-mesa-glx libgl1-mesa-dri xserver-xorg-core 
$ sudo mv /etc/X11/xorg.conf /etc/X11/xorg.conf.backup
# ln -s /usr/lib/mesa-diverted/x86_64-linux-gnu/libGL.so.1  /usr/lib/libGL.so.1


$ glxinfo
name of display: :0 
Error: couldn't find RGB GLX visual or fbconfig

#reboot
# apt-get install --reinstall xorg


http://ubuntuforums.org/showthread.php?t=952666
https://packages.debian.org/jessie/libglu1-mesa
http://wiki.cchtml.com/index.php/Debian#Removing_Catalyst.2Ffglrx

2016年4月14日

Linux 安裝Qt


下載Qt網址
http://download.qt.io/official_releases/qt/

我下載的是
http://download.qt.io/official_releases/qt/5.4/5.4.1/qt-opensource-linux-x64-android-5.4.1.run

安裝需要套件
編譯需要的一些套件            build-essential(dep:gcc, g++, make等)
Debugger                          gdb
GLX development files       libgl1-mesa-dev
# apt-get install build-essential libgl1-mesa-dev gdb

進入下載目錄
# cd /home/clementyan/Downloads/

更改檔案權限,增加可執行的權限
# chmod +x qt-opensource-linux-x64-android-5.4.1.run

執行安裝程式
# ./qt-opensource-linux-x64-android-5.4.1.run

新增qtcreator連結檔,連結到/opt/Qt5.4.1/Tools/QtCreator/bin/qtcreator
# cd /usr/local/bin/
# ln -s /opt/Qt5.4.1/Tools/QtCreator/bin/qtcreator qtcreator

一般使用者執行qt IDE
$ qtcreator

檢查compiler和gdb是否有抓到


2016年4月13日

Linux 上傳檔案到 Gist GitHub


platform:Debian Jessie x64 PC

安裝gist
# apt-get install gist

登入gist帳密
$ gist-paste --login
Obtaining OAuth2 access_token from github.
GitHub username: clementyan
GitHub password:
Success! https://github.com/settings/applications

檢查
$ ls -la | grep gist
-rw------- 1 clementyan clementyan 40 Apr 13 21:27 .gist

上傳檔案
$ gist-paste linux_kernel_source_tree.txt -f kernel_source_tree.txt -o

說明
-o, --open Open the resulting URL in a browser.
-f, --filename [NAME.EXTENSION] Sets the filename and syntax type.
--login Authenticate gist on this computer.

Ref:gist-paste -h