Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Xiaobai is prone to errors in the operation of absolute and relative paths.

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobai is prone to errors in the operation of absolute and relative paths.

As an unstable rookie, he has been messing around on that road all day. Coincidentally, there is a question in the teacher's course topic except the file of / etc/skles. And. Copy it to / home/USRNAEM 's home directory. Then he thought he was self-righteous and did not follow the teacher's method and used such an order by himself.

[root@local skel] # ls-A.bash_logout .bash _ profile .bashrc [root@local skel] # cp $(ls-A) / home/mao1/ [root@local skel] # cd / home/mao1 [root@local mao1] # ls-A.bash_logout .bash _ profile .bashrc

Then I set up a directory under the skel directory to make sure that the directory can be referenced in this way.

[root@local skel] # mkdir .test.dir [root@local skel] # mkdir test1.dir [root@local skel] # ls-A.bash_logout .bash _ profile .bashrc test1.dir .test.dir [root@local skel] # cp-r $(ls-A) / home/mao1cp: overwrite `/ home/mao1/.bash_logout'? Ycp: overwrite `/ home/mao1/.bash_profile'? Ycp: overwrite `/ home/mao1/.bashrc'? Y [root@local skel] # ls-a / home/mao1/. .. .bash _ logout .bash _ profile .bashrc test1.dir .test.dir

It also works, and then I toss it to the next user's home directory, mao2 (before it is mao1, please note). Carry out this order again.

[root@local ~] # cd / home/mao2/ [root@local mao2] # ls-A [root@local mao2] # cp-r $(ls-A / etc/skel/) / home/mao2/cp: cannot stat `.bash_logout': No such file or directorycp: cannot stat `.bash_profile': No such file or directorycp: cannot stat `.bashrc': No such file or directorycp: cannot stat `test1.dir': No such file or directorycp: cannot stat `.test.dir': No such file or directory

Unexpectedly gave me feedback can not find files and directories, I was instantly confused. Why? The exact same operation, unexpectedly before can be executed to change the directory will make an error. I feel like I'm out of love in an instant. Then Lao Wang next door asked me to make mistakes and was born as a trouble maker. Take a wrong path that does not belong to me, all kinds of Baidu, google can not solve for me. Then the man documents of cp and ls were translated one by one, but there were no results. Immediately think of "a cigarette, solve a thousand sorrows, go to hate." Sure enough, when I came back, I looked through the ppt and found the absolute path and the relative path. Take a closer look, although I ls-A / etc/skel shows the files under the skel directory, but when I cp is not in the skel directory, the system will look for these files under the current path, coincidentally, the current directory does not have these files, so something went wrong. Ladies and gentlemen, you should see that I made a mistake here. Next, check to see if my idea is correct.

[root@localhost tmp] # ll-Atotal 0 [root@localhost tmp] # cd / etc/skel/ [root@localhost skel] # ls-A.bash_logout .bash _ profile .bashrc .mozilla [root@localhost skel] # cp-r $(ls-A / etc/skel/) / tmp [root@localhost skel] # cd / tmp/ [root@localhost tmp] # ll-Atotal 12Murray RWLY. 1 root root 18 Aug 2 08:44 .bash _ logout-rw-r--r--. 1 root root 193 Aug 2 08:44 .bash _ profile-rw-r--r--. 1 root root 231 Aug 2 08:44. Bashrcdrwxr-xr-x. 4 root root 37 Aug 2 08:44. Mozilla [root@localhost tmp] # cd / home/mao2 [root@localhost mao2] # ll-Atotal 0 [root@localhost mao2] # cd-/ tmp [root@localhost tmp] # cp-r $(ls-A / etc/skel/) / home/mao2/ [root@localhost tmp] # cd-/ home/mao2 [root@localhost mao2] # ls-A.bash_logout .bash _ profile .bashrc. Mozilla [root@localhost mao2] #

As you can see above, as long as we can succeed in the directory with these files, let's go to the directory where there are no these files and execute the following command.

[root@localhost mao2] # cd / usr/tmp/ [root@localhost tmp] # ls-A [root@localhost tmp] # cp-r $(ls-A / etc/skel/) / home/mao2/ cp: cannot stat '.bash_logout': No such file or directorycp: cannot stat' .bash_profile': No such file or directorycp: cannot stat '.bashrc': No such file or directorycp: cannot stat '.mozilla': No such file or directory

If you don't deceive me, you will not succeed. Continue to toss about as a rookie, there is no path, I will add it to you.

[root@localhost tmp] # cp-r / etc/skel/$ (ls-A / etc/skel/) / home/mao2/ cp: overwrite'/ home/mao2/.bash_logout'? Ycp: cannot stat '.bash_profile': No such file or directorycp: cannot stat' .bashrc': No such file or directorycp: cannot stat '.Mozilla': No such file or directory

Oh,shit . You can only copy one. When you see that there are spaces in the middle, the system regards each space as a relative path. No, let's go on, anyway, as a trouble maker. I just keep making trouble. Go ahead, in that case I'll try to output the results with xargs, and then use sed to add a path to each file.

Try 1: [root@localhost tmp] # cp-r / etc/skel/ {$(ls-A / etc/skel/ | xargs | sed-r "s # [: space:]] #\, # g")} / tmpcp: cannot stat'/ etc/skel/ {.bash_logout,.bash_profile,.bashrc,.mozilla}': No such file or directory [root@localhost tmp] # cp / etc/skel/ {.bash_logout,.bash_profile,.bashrc .Mozilla} / usr/tmp/cp: omitting directory'/ etc/skel/.mozilla' [root@localhost tmp] # cp-r / etc/skel/ {.bash_logout,.bash_profile,.bashrc,.mozilla} / usr/tmp/cp: overwrite'/ usr/tmp/.bash_logout'? Ycp: overwrite'/ usr/tmp/.bash_profile'? Ycp: overwrite'/ usr/tmp/.bashrc'? Y

The reason for this problem is that the {} feature of bash referenced by the command is no longer recognized, and / etc/skel/ {.bashlogout, .bashprofile, .bashrc, .mozilla} is treated as a whole file. So the road is blocked.

Try 2: [root@localhost ~] # ls-A / etc/skel/ | xargs | sed-r "s # (\.. * [[: alpha:]]\ b) # / etc/skel/\ 1Secretg" / etc/skel/.bash_logout .bash _ profile .bashrc .Mozilla

This attempt is the most complicated. I have tried a variety of different methods. I will briefly write some of them below.

[root@localhost tmp] # ls-A / etc/skel/ > 1.txt [root@localhost tmp] # cat 1.txt .bash _ logout.bash_ profile.bashrc.mozilla [root @ localhost tmp] # sed-r "s # (\. * [[: alpha:]]\ >) # / etc/skel/\ 1millig" 1.txt / etc/skel/.bash_logout/etc/skel/.bash_profile/etc/skel/.bashrc/etc/skel/.mozilla

This proves that the regular expression is correct. Go on

Oh,no, another mistake.

[root@localhost tmp] # ls-A / etc/skel/ | xargs > 1.txt [root@localhost tmp] # sed-r "s # (\. * [[: alpha:]]\ >) # / etc/skel/\ 1Secretg" 1.txt / etc/skel/.bash_logout .bash _ profile .bashrc .Mozilla

So I suspected it was the xargs command, and I made an attempt with the addition operation.

[root@localhost tmp] # seq 1 10 | xargs | sed-r "s# # + # g" | Bc55 [root @ localhost tmp] # seq 1 10 | xargs 1 2 3 4 5 6 7 8 9 10 [root@localhost tmp] # seq 1 10 | xargs | sed-r "s # # + # g" 1 "2" 3 "4" 5 "7" 8 "9 # 10 [root@localhost tmp] # seq 1 10 | xargs | sed-r" s # # + # g "| bc55

There is no problem with xargs, the same command, the same regular expression doesn't work on it. If you can solve this problem, please let me know.

Try 3: [root@localhost home] # for i in `ls-A / etc/skel/ `; do cp-r / etc/skel/$i / tmp; donecp: overwrite'/ tmp/.bash_logout'? Ycp: overwrite'/ tmp/.bash_profile'? Y cp: overwrite'/ tmp/.bashrc'? Y

The idea of using for loops to do this comes from teacher Rex.

Try 4:cp-r / etc/skel/. / home/mao

The method of Lao Wang next door, this method is the most perfect and easiest way. Teacher wang is an old driver.

Summary: first, understand why troubleshooting is the most painful. It took me a day to get rid of this incomplete error for 1. 5%. As a rookie, there are a lot of things untouched, this is a job of eating experience. two。 I complicate the simple problem by myself. 3. The understanding of relative path and absolute path is not deep enough. Second, the source teacher Rex, since it does not know how to make an error, appends each command to a new file one by one, and executes the command on the appended file. Third, do your homework in a down-to-earth manner and don't be curious. Curiosity kills the cat. Fourth, if you have any questions, ask Lao Wang, the almighty teacher wang, next door.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report