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

posted on 2015-02-14 11:30  tneduts  阅读(188)  评论(1编辑  收藏  举报

导航