A bug in the shell script that defines the PATH variable

Source: Internet
Author: User
Tags rsync

===========================================================================

if define a PATH variable in a statement the path that raised the command bug where path change occurs problem

    • Original script file: INotify +rsync Live Sharing

#!/bin/bash

path=/data/

ip=172.16.1.41

CD $PATH &&\ switch to a new variable.

/usr/bin/inotifywait-mrq--format '%w%f '-e create,moved_to,close_write,delete $PATH |while read line

Do

If [-f $line];then the rsync command at this time is no longer available (workaround absolute path)/usr/bin/rsync

Rsync-az $line--delete [email protected] $IP:: Nfsbackup--password-file=/etc/rsync.password

Else

CD $PATH &&\

Rsync-az./--delete [email protected] $IP:: Nfsbackup--password-file=/etc/rsync.piassword

Fi

Done

===========================================================================

    • Sh-x the validation process

[Email protected] scripts]# vim jiankong2.sh

#cd $PATH &&\

Do

If [-F $line];then

echo 01 specifically adds the display content to the current variable path in a convenient location

Echo $PATH

echo 03

Rsync-az $line--delete [email protected] $IP:: Nfsbackup--

Password-file=/etc/rsync.password

Else

echo 01

Echo $PATH

Sh-x Viewing process

[Email protected] scripts]# sh-x jiankong2.sh

+ path=/data/

+ ip=172.16.1.41

+ Read Line

+/usr/bin/inotifywait-mrq--format%w%f-e create,moved_to

+ ' ['-f/data/oldgirl '] '

+ Echo 01

01

+ The path of the Echo/data/path variable is converted to/data/so the command reads the path

/data/

+ Echo 03

03

+ rsync-az/data/oldgirl--delete [email protected]:

Jiankong2.sh:line 13:rsync:command Not found conclusion: if the error command cannot be found

+ Read Line

+ ' ['-f/data/oldgirl '] '

+ Echo 01

01

+ echo/data/

/data/

+ Echo 03

03

+ rsync-az/data/oldgirl--delete [email protected]:

Jiankong2.sh:line 13:rsync:command not found the same command cannot be found

+ Read Line

===========================================================================

    • Workaround One define the variable to and path Differentiate Open Delete CD to the variable path

[email protected] scripts]# cat jiankong2.sh

#!/bin/bash

weizhi=/data/

ip=172.16.1.41

#cd $PATH &&\

/usr/bin/inotifywait-mrq--format '%w%f '-e create,moved_to,close_write,delete $WEIZHI |while read line

Do

If [-F $line];then

Rsync-az $line--delete [email protected] $IP:: Nfsbackup--password-file=/etc/rsync.password

Else

#cd $PATH &&\

Rsync-az $WEIZHI--delete [email protected] $IP:: Nfsbackup--password-file=/etc/rsync.password

Fi

Done

===========================================================================

    • Workaround Two

Use absolute paths to jump out of the current defined path environment

/usr/bin/rsync

===========================================================================

A bug in the shell script that defines the PATH variable

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.