Standard Input/Output is defined/initialized when unix system is launched. We can redirect the standard input / output in script.
test:
123Hello 456 world!
terminal script:
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ tr -d [:digit:] <./test
Hello world!
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ tr -d [:digit:] <./test >./test_fil
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cat ./test_fil
Hello world!
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ man tr
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ tr [:lower:] [:upper:] <./test >./test_fil
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cat ./test_fil
123HELLO 456 WORLD!
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ tr -d [:digit:] <./test >>./test_fil
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ cat ./test_fil
123HELLO 456 WORLD!
Hello world!
Explanation:
1) tr -d [:digit:] <./test: we are redirecting standard input to ./test, it means, for all text in ./test, delete all digit numbers defined in local unix system, and print it out to standard output.
2) tr -d [:digit:] <./test >./test_fil, everything is same, but we redirect standard output to ./test_fil, so after "cat" ./test_fil, we can see the same output
3) tr [:lower:] [:upper:] <./test >./test_fil. We are replacing all lower case characters in ./test with appropriate upper case character, and outputting the result to ./test_fil. At this time, ./test_fil already existed, so its content got truncated.
4) tr -d [:digit:] <./test >>./test_fil, everything is same, but we "appended" into ./test_fil. That's why we see ./test_fil still have the content output from above.
2. /dev/null
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ tr -d [:digit:] <./test >/dev/null
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$
/dev/null is the "bit bucker". All information output to /dev/null will be just thrown away by the system.
3. /dev/tty
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ tr -d [:digit:] </dev/tty
Hello 123 world
Hello world
/dev/tty means, system must direct the input to "terminal", which means application must let user to put in the input information.
Above command let user put in string "Hello 123 world", and delete the number then output result to standard output - terminal
4. Command Searching:
Whenever user input a command at the shell, unix will search the command from directories indicated by $PATH system variable. Example:
num_users:
#! /bin/bash
set -x
who | wc -l
set +x
test:
#! /bin/bash
num_users #output command not found
echo $PATH
#output: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PATH=$PATH:$HOME/Desktop/xxdev #Add current path to $PATH
echo $PATH #output normal path string + current path
#output: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/aubinxia/Desktop/xxdev
num_users # ok to run now
#output:
#+ who
#+ wc -l
#2
#+ set +x
terminal:
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ ./test
./test: line 3: num_users: command not found
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/aubinxia/Desktop/xxdev
+ who
+ wc -l
2
+ set +x
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/aubinxia/Desktop/xxdev
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Note: even if we run ./test, which modified the $PATH system variable. But when returning to shell, $PATH doesn't change! So when shell start a new process to run the script, it probably just "copy" the $PATH variable to the process.
5. Simple Execution Tracing
num_users:
#! /bin/bash
who | wc -l
terminal:
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ bash -x num_users
+ wc -l
+ who
2
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ bash +x num_users
2
bash -x means: turn on the execution tracing, we could the trace for every line of our script running.
bash +x means: turn off the execution tracing, we could only see the result in this case.
=====================================
num_users:
#! /bin/bash
set -x
who | wc -l
set +x
terminal:
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ ./num_users
+ who
+ wc -l
2
+ set +x
set -x: means turn on the execution tracing
set +x: means turn off the execution tracing
At the terminal side, it didn't output the tracing for set -x, because the execution tracing is not on until this command get finished. It outputs "set +x" because the execution tracing is not off until this command get finished.
6. Internationalization and Localization
Internationalization: software adapt to global standard, making global users use software without needing to change or re-compile the code.
Localization: translate the software, including the character library, documentation, manual etc.
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
locale command could list all unix environment variables controlling the language issues.
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ locale -a
C
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
POSIX
locale -a could show up all available locale options in the current environment.
7. Access Script Arguments
Because of history reason, for arguments who have more than 1 digit, we should use ${xx}.
test:
#! /bin/bash
echo $1
echo $2
echo ${10}
terminal:
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ ./test 1 2 3
1
2
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ ./test a b c d e f g h i j
a
b
j
In the first command, we didn't provide enough arguments, so echo ${10} just output a space.
No comments:
Post a Comment