advertisement

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年6月29日

台北市 北市 市政府 市民服務電話 聯絡 聯繫方式 客服 疫苗施打 臺北

 臺北市民當家熱線 (02)2720-8889


歡迎加入台北市政府Line好友
https://line.me/R/ti/p/@taipei?from=page


以及下載台北通APP
https://id.taipei/tpcd/about/taipeipass-app


以利掌握北市最新資訊,並便於使用實聯制與各項服務 👍


台北市民關心您😉

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

2018年7月11日

真誠的讚美 帶給雙方美好的感受

如何真誠讚美一個人?

真誠讚美一個人要先有顆真誠的心,願意花時間仔細努力的觀察一個人的優點,找出優點甚至是跟別人不一樣突出的優點,給予讚美真誠的欣賞他


什麼叫不一樣突出的點

簡單舉
在禮堂台上演講者發問問題,讓台下的聽眾來舉手回答,等了一會兒有位人舉了手回答這沒人回答的問題,並且答案完全正確!!在休息時間裝水的時候,我剛好遇到他,我就說:嗨~你怎麼知道剛剛那題的答案呀OAO!! 他跟你說明之後原來他很熱愛看戰爭片所以他很熟這問題,我就覺得:哇賽! 現場那麼多人只有你答得出來耶 好猛喔OAO! 


任何人都一定有優點!!

當你學會觀察別人的優點後,其實會發現任何人都有優點~
簡單舉
身邊的同學有什麼優點? 成績好、很會聊、很會打球、願意無私分享知識。
在更小的事情~
我發現你不但不會隨手丟垃圾,只要你路過看到地上有垃圾都會主動撿起來。
玩遊戲的時候~我仔細觀察發現他玩遊戲滑鼠移動看起來好可愛哦O口O!! 看起來像撲撲車的感覺!!

那那如果是剛出生的小寶寶有什麼優點?  
.
.
.
.
.

2018年7月7日

嫉妒 可以誹謗一個人 也可以讓自己成長

嫉妒?

人類有多達27種情緒,簡單分喜、怒、哀、樂,細分還有渴望、嫉妒、同情等,嫉妒是怎麼樣的一種情緒~

只要是正常人都會有嫉妒之心,嫉妒主要對象是人,我們會嫉妒一個人的好,一個人的優點或快樂,卻不會嫉妒一個人的缺點或不快樂,嫉妒屬於七宗罪對人類惡行的之一,在嫉妒心的作用之下,人可能會透過謠言、搬弄是非、誹謗和其他間接的行為以貶損被嫉妒的人,而無意之間造口業。



不好的行為

口業不論是在道教、佛教、一貫道或基督教各宗教都共同指出一個方向(Google很多),不能口出惡言、散布謠言或搬弄是非詆毀他人,應該說好話,如果是無神論者,我可以跟你說有因就會有果,做了惡劣行為會不會有報應我沒見證過我不知道,也許有些人所說的報應只是人給予加害人的另一種回報,但我可以確定的是有因就會有果,有原因才會有結果,這也是在自然界科學都很常見的。

簡單舉如果一個人一天到晚都在想要怎麼陷害那個人要怎麼到處詆毀那個人,可能你會花很多心思在策劃如何陷害他,而當你一開口到處散佈不實謠言詆毀他人,別人對你的第一印象就是你很愛說說別人的壞話或毀謗他人,而這樣的結果不僅浪費時間在思考怎麼陷害一個人,一個經常到處口出惡言,會有多少人喜歡他呢?
相反的一個人仔細觀察一個人看出一個人的好,用真誠的心來讚美他,到處讚美別人說好話,別人想到你的第一瞬間不會是惡事,而是你的好話!
真誠的讚美

被害人的影響

這些惡事會帶給受害者很深的影響,一個被嫉妒的人常常會因為擔心「嫉妒」給自己帶來的人際關係的負面影響而感到不安,也因為害怕自己成為眾矢而感到惶恐,這種擔憂和顧慮造成他們巨大的心理折磨。


好的行為

嫉妒是複雜的情緒,我們要如何利用嫉妒的心來讓自己成長、讓自己進步~
仔細觀察他人找出他的優點向他學習或模仿來提升自我,讓自己成長、讓自己進步,嘗試讓自己接近或達到被嫉妒的人的成就,或者在其他方面能夠更勝於他,來緩和嫉妒的情緒。 簡單舉例如果有個英文很好的同學每次段考英文總是拿第一,而你是位很努力很拼命的人,不管再怎麼努力英文還是無法贏過他,仔細觀察為什麼他的英文總是這麼好,他的優點是什麼,向他學習他的優點或請教他,或許他從小在國外受教育所以英文對他來說易如反掌,那先天的優勢我們可以反過來思考我們自身的優勢,加強自身的強項來戰勝他人,達到自我成長也是可以的。




正向思考

當別人嫉妒你表示你實力很強,當別人抹黑搬弄是非表示他們不願意承認你的好,他們不願意承認他們自己錯了,他們是在幫你消業障,即使全世界沒人相信你,只要有個好朋友願意相信你就足夠,不管有多少人想要惡意扭曲抹黑一個人,問心無愧永遠堅持做一個正直的人不去傷害人堅持到底!


自我的影響

好的嫉妒會讓人有一種被他人的優勢或成功所激勵的感覺,可以讓自己成長許多,邁向成功的一步。

兩舌

兩舌則是利用機會在兩人之間道長話短並搬弄是非,最終目的在於挑撥離間,破壞當事人的處事和諧。

在日常生活中,犯了這四項口業,其惡口傷害了對方感情,妄言破壞了當事人人格,而搬弄是非及綺語亦使彼此相處無法真誠對待。 ref: https://zh.wikipedia.org/wiki/%E5%8F%A3%E6%A5%AD

2017年4月10日

How to choose right PWM frequency

The PWM switching frequency has to be much higher than what would affect the load (the device that uses the power), which is to say that the resultant waveform perceived by the load must be as smooth as possible. The rate (or frequency) at which the power supply must switch can vary greatly depending on load and application, for example
Switching has to be done several times a minute in an electric stove; 120 Hz in a lamp dimmer; between a few kilohertz (kHz), to tens of kHz for a motor drive; and well into the tens or hundreds of kHz in audio amplifiers and computer power supplies.


For a question like this, you will probably get as many answers as there are people interested in answering. Here is my answer: It depends.
Here are some of the limiting factors, first the lower limits:
  • Persistence of vision:
    • Different people are differently sensitive to flicker in a light source. Some would notice flicker even at 100 Hertz, others perhaps not even at as low as 10 Hz.
    • Motion of light source relative to the eye makes flicker more discernible, scaling up with speed of the motion.
    • Human vision sensitivity at low intensity of light - both ambient and source intensity. At very low intensity, the eye is much more sensitive to any change in intensity. So an LED operated at low duty cycle / low current and in a dark environment would require a higher minimum PWM frequency.
Now the upper limits:
  • LED turn-on characteristics: An LED cannot be toggled at arbitrarily high frequency, once the pulse duration approaches the turn-on time, the LED never really turns on fully, hence linearity of PWM control is lost to begin with, and at higher frequency / shorter pulses, eventually the LED just stays dim or off.
  • PWM provider capabilities: Your microcontroller would have its own maximum PWM rate, which sets a hard limit.
  • Switching losses: Any switching system, MOSFET based, BJT based, or other, suffers switching losses of power as switching rate increases. At one point this become significant both in terms of heating of switching device, and efficiency of illumination.
Thus, depending on these parameters, and any others affecting your specific requirement, the correct answer could be anywhere in the 50 Hz to few dozen KHz range.

motor frequency 可以測試不同頻率下輸出電流來決定

Ref: