conditions elif | sed -n q;d awk


cat rd
if [ -r "$1" ]; then
  echo "$1 is readable"
else
  echo "Error to read $1"
fi
$ bash rd file
file is readable
$ bash rd file1
Error to read file1
--------------
cat el
OS=`uname -s`
if [ "$OS" = "Linux" ]; then
  echo "This is Linux"
elif [ "$OS" = "Solaris" ]; then
  echo "This is Solaris"
else
  echo "This is Unix"
fi
$ bash el
This is Linux
--------------
if [ -e /etc/resolv.conf ]; then
-e stands for exists
-L stands for symbolic link
-S stands for socket
-p stands for pipe
-d stands for directory
 
--------------
#test symbolic link
$ cat sy
file=$1
if [ -L $file ]; then
  echo "this is symbolic link"
else
  echo "not identified file"
fi
$ bash sy filesym1
this is symbolic link
--------------
Replace particular line 3:
sed 3s/echo/read/g el > el3
Print line 3:
cat el | awk 'NR==3{print}'
Print column 3:
cat el | awk '{print $1}'
#print line number 2
sed '2q;d' file # method efficient for large files
#other methods, print lines 2 and 3
sed '2,3!d' file
#tell sed to delete all but the second and third lines
Alternative method for print lines from 2 to 4
#
sed -n 2,4p file
sed -n -e 1p -e 3,4p file
#print lines 1, 3 and 4
---------------------
cat file2
some text
new text
aaaa text
bbbb cccc
new ddddd
awk '$1=="new"{$2="NEW"}1' file2
some text
new NEW
aaaa text
bbbb cccc
new NEW
---------------------
$ cat comp
file1=$1
file2=$2
ls $file1 $file2 -il
if [ $file1 -ef $file2 ]; then
  echo "files are similar"
else
  echo "$file1 is not the same as $file2"
  diff -q $file1 $file2
    if [ "$?" -eq "0" ]; then
      echo "contents is the same"
    fi
fi
--------------------
$ bash comp file1 file3
2130443 -rw-r--r-- 2 x 10 Jul 23 13:00 file1
2130443 -rw-r--r-- 2 x 10 Jul 23 13:00 file3
files are similar
$ bash comp file1 file2
2130443 -rw-r--r-- 2 x 10 Jul 23 13:00 file1
2130444 -rw-r--r-- 1 x 10 Jul 23 13:00 file2
file1 is not the same as file2
contents is the same
---------------------
if [ new –nt old ]; then #true if new is newer than older file
if [ new –ot old ]; then #true if new is older than older file
---------------------
Bash compare 2 strings
If [ “$1”=”$2” ]; then
Bash shell, and other, will also accept a double ==, which is more in line with other languages
However traditional Bourne shell does not recognize this syntax. It’s best to use single = as that is understood everywhere
For inequality
if [ “$1”!=”$2” ]; then
---------------------
If you want to change a pathname that contains a slash - say /usr/local/bin to /common/bin - you could use the backslash to quote the slash:

sed 's/\/usr\/local\/bin/\/common\/bin/' <old >new
Gulp. Some call this a 'Picket Fence' and it's ugly. It is easier to read if you use an underline instead of a slash as a delimiter:

sed 's_/usr/local/bin_/common/bin_' <old >new
Some people use colons:

sed 's:/usr/local/bin:/common/bin:' <old >new
Others use the "|" character.

sed 's|/usr/local/bin|/common/bin|' <old >new

Comments

Popular posts from this blog

HAproxy logging

teamcity Automatic Agent Start under Linux

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all