Shell basic1

A shell script is a text file that typically begins with a shebang, as follows:

#!/bin/bash

/bin/bash is

the interpreter command path for Bash.

$ sh /home/path/script.sh # Using full path of script.sh.

chmod a+x script.sh

#character is used to denote the beginning of unprocessed comments.

For every

process, environment variables in its runtime can be viewed by:

cat /proc/$PID/environ

cat /proc/12501/environ

cat /proc/2849/environ | tr ‘\0‘ ‘\n‘

Then you can split the environment string to property=value format.

Judge the current user is super user or not:

if [ $UID -ne 0 ]; then

echo Non root user. Please run as root.

else

echo "Root user"

Fi

File descriptors are integers that are associated with file input and output. They keep track

of opened files. The best-known file descriptors are stdin, stdout, and stderr.

0 – stdin(standard input)

1 – stdout(standard output)

2 – stderr(standard error)

#!/bin/bash

#Description: Illustration of IFS

line="root:x:0:0:root:/root:/bin/bash" 

oldIFS=$IFS;

IFS=":"

count=0

for item in $line;

do

[ $count -eq 0 ] && user=$item;

[ $count -eq 6 ] && shell=$item;

let count++

done;

IFS=$oldIFS

echo $user\‘s shell is $shell;
  • -gt: Greater than
  • -lt: Less than
  • -ge: Greater than or equal to
  • -le: Less than or equal to

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。