Showing posts with label echo. Show all posts
Showing posts with label echo. Show all posts

Wednesday, December 30, 2020

how to convert from one date format to another format in linux ?

 how to convert from one date format to another format in linux ?

echo 04-11-2021 | { IFS=- read d m y && echo "$y$m$d"; }

20211104


echo 04-11-2021 | { IFS=- read d m y && echo "$y/$m/$d"; }

2021/11/04


 echo 04-11-2021 | { IFS=- read d m y && echo "$d$m$y"; }

04112021


 echo 04-11-2021 | awk -F- '{print $3$2$1}'

20211104


echo 04-11-2021 |gawk -F, '{split($1, a, "-"); print a[3] a[2] a[1]}' 

20211104


echo 04/11/2021 | { IFS=/ read d m y && echo "$y$m$d"; }

20211104


date -d "20211104" "+%Y-%m-%d"

2021-11-04


date -d 04/11/2021 +%F

2021-04-11


 %F   full date; same as %Y-%m-%d


Monday, November 23, 2020

how to check If the file Is Empty or not using shell script?

 how to check If the file Is Empty or not using shell script?


-s FILE

              FILE exists and has a size greater than zero


Example 1:


touch /$HOME/f1


echo "data" >/$HOME/f2

ls -l /$HOME/f{1,2}

[ -s /$HOME/f1 ] 

echo $?


Sample outputs:


1



The non-zero output indicates that the file is empty.


[ -s /$HOME/f2 ]

echo $?

Sample outputs:


0


$? is the return code of the last executed command.


Example 2:


$ ls

$ touch file_1

$ dd if=/dev/zero of=$HOME/file_2 bs=1 count=100

100+0 records in

100+0 records out

$ ls -l file_1 file_2 file_3

ls: file_3: No such file or directory

-rw-rw-r--  1 foo bar   0 Nov 20 20:28 file_1

-rw-rw-r--  1 foo bar 100 Nov 20 20:28 file_2

$ [[ -s file_1 ]]

$ echo $?

1

$ [[ -s file_2 ]]

$ echo $?

0

$ [[ -s file_3 ]]

$ echo $?

1