Summary of Shell Small issues

Source: Internet
Author: User
Tags time zones local time shebang

Summary of Shell Small issues

This article source: http://blog.csdn.net/bluishglc/article/details/44276607 prohibited any form of reprint, or will entrust CSDN official maintenance rights!

    • Summary of Shell Small issues
      • If multi-conditional combination judgment example
      • Linux System Time settings
      • Push commands on a remote node requires special attention and
      • Analysis of difference between sudo-i and su-l
      • About Varrun and PID
      • File Test Parameters Summary
      • String Test Parameter Summary
      • How to correctly pass array parameters
      • Batch Modify file name
      • Configuring scripts for 163 yum Repo
      • About Devnull 21devnull
      • Standard input and output and errors
      • The string manipulation
      • Example of string manipulation
      • Background Run Nohup
      • Condition testing Test
      • Condition test
      • Pre-defined variables

If multi-conditional combination judgment example
if"$STARTUP_TARGET""all"-a"$STARTUP_TARGET""hadoop"-a"$STARTUP_TARGET""spark" ]then   echo"STARTUP_TARGET is invalid or not specified!"   exit1fi

Note: For if, the immediately following "[" is actually the alias of the test command! So the subsequent expression is the parameter of the test command, so you will need:

    • "[" to the right to add a space followed by the parameter
    • "=" requires a space on both sides

Otherwise the parameters are joined together and become a parameter!

Linux System Time settings
    1. Modify Time Zone
      Many systems do not have time zones set up during installation, so many systems use UTC to display the time by default, using date to look at the output time to know if the time zone and time are correct (if UTC is uniform, non-local time zone, if CST represents Chinese standard Time, means that the time zone is normal). Usually, the time is not correct more than because of the time zone problem, so we just need to modify the system's time zone, by overwriting the local (Shanghai) time zone file to/etc/localtime:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

2. Modify the Ls-l time format

vim /etc/profile.d/time_style.shaddexport TIME_STYLE="+%F %T"
Special attention is required when pushing commands on remote nodes$! and$?
ssh -T [email protected]<<EOF    -l"nohup $HIVE_HOME/bin/hive --service metastore -hiveconf hive.log.file=hive-metastore.log     -hiveconf hive.log.dir=$HIVE_LOG_DIR>$HIVE_LOG_DIR/hive-metastore.out 2>$HIVE_LOG_DIR/hive-metastore.log &"    -l"echo $!|cat>$HIVE_PID_DIR/hive-metastore.pid"EOF

$!the value in the above script is always the current host or the PID of the last daemon under the local shell, but not the pid! of the previous push. hive --service metastore

Analysis of difference between sudo-i and su-l
-T [email protected]$NAME_NODE<<-i-u$HDFS_USER$HADOOP_HOME-format-forceto run sudo

This error requires the configuration to be modified via Visudo, if instead:

-T [email protected]$NAME_NODE<<-l$HDFS_USER-c"$HADOOP_HOME/bin/hdfs namenode -format -force"EOF

Then no problem, summed up: Su-l than sudo-i easy to use!

Add: Although the use of SU is very convenient, but the disadvantage is that when my host is a multi-person condominium environment, if you want to use SU to switch to the identity of root, then everyone needs to know the root password, so the password too many people know may flow out, very inappropriate! What do you do? It can be handled by sudo! As opposed to SU needs to know the new Switch user password (often requires root password), sudo operation only need their own password! It can even be configured to run sudo without a password!

About/var/run and PID

Well Since/var/run is mounted as TMPFS. That means it's totally empty when your machine boots and it's meant to being like this to prevent stuff like daemons not STA Rting because of a left-over pid-file.

Startup scripts usually create the directories they need before using them. If you want to store a pid-file either put it in/var/run directly or create a directory before creating the Pid-file. This was no place to store data, needs to remain there across reboots.

    1. /var/run is where the Linux convention application stores PID files (most systems have changed to/run, and then/var/run to soft Connect to/run), which is not a special folder, and Linux does not force the application to put PID here. Just say this is an "agreed, default" location for storing PID files! The only special thing about this folder is that, under most current Linux systems, it is mounted in order to TMPFS partitions, which means that all data in the folder will be emptied as soon as the system restarts
    2. First we need to understand: the PID file itself and the Linux operating system is not necessarily related to the production and use of the application is responsible for the self! In general, the canonical application at startup will find its own PID file under the specified folder, and if the file exists stating that the program is running, terminate the current startup program and give the prompt message! If it does not exist, it will continue to start. When the program is closed, you will also delete the PID file yourself! Of course, the above is said to the "specification" of the application, and some procedures do not necessarily follow such a specification operation, which requires specific analysis of the situation.
File Test Parameters Summary

String Test Parameter Summary

How to correctly pass array parameters
#!/bin/sht(){    nodeList="$1"    echo${nodeList[*]}}a=(123${a[*]}

Output: 1

#!/bin/sht(){    nodeList="$1"    echo${nodeList[*]}}a=(123"${a[*]}"

Output: 1 2 3

Batch Modify file name

For example: Add the Bak suffix to the/etc/yum.repos.d centos repo file:

forin$(ls CentOS-*)do$i$i.bak;done
Configuring scripts for 163 yum Repo
cd /etc/yum.repos.dforin$(ls CentOS-*)do$i$i.bak;donewget http://mirrors.163.com/.help/CentOS6-Base-163.repo
About ">/dev/null 2>&1", ">&/dev/null"
id user >&/dev/nullif0 ]then-s$userfi

Above is a section to determine whether the user exists, if not exist to add the user's script, where >&/dev/null equivalent to >/dev/null 2>&1 and >/dev/null 2>&1 is equivalent to 1>/ Dev/null 2>&1, they mean the same thing: redirect The standard output (that is, file 1, which represents the standard output) to the/dev/null (that is, the standard output information is not printed), Then the standard error output (that is, file 2 representing the standard error output) is combined (that is, the meaning of the & symbol) to redirect to the standard output , so that the resulting effect or goal is to discard all the information that is output from the command execution (no information is printed)!

In the POSIX shell, the result of the command can be defined in the form of%> (where% represents the file descriptor: 1 is the standard output stdout, 2 is the standard error stderr)! System default% value is 1, that is 1>, and 1> can be abbreviated to >, also is the default is >.

>&/dev/nullThis is >/dev/null 2>&1 an abbreviation for the right!

Standard input, output, and error

When we execute commands in the shell, each process is associated with three open files and uses file descriptors to refer to the files. Because the file descriptor is not easy to remember, the shell also gives the corresponding file name. Here are the file descriptors and the filenames they typically correspond to:

file File Descriptor
Input file-Standard input 0
Output file-standard output 1
Error output file-standard error 2

There are actually 12 file descriptors in the system, but as we can see in the table above, 0, 1, 2 are standard input,
Output and errors. You can use file descriptors 3 to 9 arbitrarily.

String manipulation of ${}

Example of string manipulation for ${}

File=/dir1/dir2/dir3/my.file.txt
We can replace each other with ${} to get different values:

${file#*/}:拿掉第一条/及其左边的字符串:dir1/dir2/dir3/my.file.txt${file##*/}:拿掉最后一条/及其左边的字符串:my.file.txt${file#*.}:拿掉第一个.  及其左边的字符串:file.txt${file##*.}:拿掉最后一个.  及其左边的字符串:txt${file%/*}:拿掉最后条/及其右边的字符串:/dir1/dir2/dir3${file%%/*}:拿掉第一条/及其右边的字符串:(空值)${file%.*}:拿掉最后一个.  及其右边的字符串:/dir1/dir2/dir3/my.file${file%%.*}:拿掉第一个.  及其右边的字符串:/dir1/dir2/dir3/my${file:0:5}:提取最左边的5个字节:/dir1${file:5:5}:提取第5个字节右边的连续5个字节:/dir2${file/dir/path}:将第一个dir替换为path:/path1/dir2/dir3/my.file.txt${file//dir/path}:将全部dir替换为path:/path1/path2/path3/my.file.txt

The methods of memory are:

#是去掉左边(在鉴盘上#在$之左边)%是去掉右边(在鉴盘上%在$之右边)单一符号是最小匹配﹔两个符号是最大匹配
Background Run nohup...&

Unix/linux generally like to let a program run in the background, many use & at the end of the program to let the program run automatically. For example we want to run MySQL in the background:
/usr/local/mysql/bin/mysqld_safe–user=mysql &
But join us many programs do not like Mysqld as the Guardian process, perhaps our program is only ordinary program, generally this program uses & end, but if the terminal is closed, then the program will be closed. But in order to be able to run in the background, then we can use the Nohup command, such as we have a test.php need to run in the background, and want to be able to run periodically in the background, then use Nohup:
Nohup/root/test.php &

Condition test: test,[]

There are three types of test conditions in the shell: test,[] and (()). where test and [] are equivalent, "[" is actually just an alias of the test command! That's why [there must be a space after that]. Therefore, they are grouped into a class of discussions.

[ ]

[] There are three types of test objects:

String, such as [ -z str ]
Numbers, such as [ n1 -lt n2 ]
files, such as[ -f filename ]

variables within [], preferably enclosed in double quotation marks, in brackets, preferably in single or double quotation marks, for example

if${var} ]  #可能出错  if"${var}" ]  #最好改成这样  

-A (and) and-O (OR): Compound tests for multiple conditions, such as

if"${var}"-a"${var}"-lt100#当变量被定义且其值小于100时

Take Back test!: Reverse the test condition

if"$?"-eq"0" ]  #上一条命令返回值不等于0  
Condition test: (())

1. (()) When testing conditions are used, the digital test conditions of Class C

< :小于> :大于<= :小于等于>= :大于等于== :等于!= :不等于

2. After testing, (()) can not be used-a,-o and! etc Compound test

3. (()) In addition to being used as a numerical test, can also be used for variable self-increment, such as ((var++))

Pre-defined variables

Summary of Shell Small issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.