Ошибки при выполнении сценария bash: разрешение отклонено; Нет такого файла или каталога

Я только начинаю работать с Ubuntu. Я пытаюсь создать скрипт, который будет делать следующее:

  1. Выполните ls -al для файлов в домашнем каталоге пользователя, а затем сохраните его в файл в моем каталоге.
  2. Я хочу сохранить информацию о дереве для домашнего каталога в файл. Он называется tree.dat в моем каталоге.
  3. Создайте новый каталог в моем домашнем каталоге с именем backups .
  4. Переместите созданные мной файлы в этот новый каталог.

Проблема, с которой я работаю Я продолжаю получать ошибку «Отказано в разрешении». Он говорит мне, что мой домашний каталог не существует, хотя и должен.

Вот мой скрипт:

#!/bin/bash
cd /home
ls -al > ls.txt
tree/home>tree.txt
mkdir backups 
cd backups 
mv -t /home/backups ls.txt tree.txt
mv ls.txt ls.bu
mv tree.txt tree.bu

Это вывод:

luse@luse-VirtualBox:~$ sudo 777 script1.sh
sudo: 777: command not found
luse@luse-VirtualBox:~$ chmod 754 script1.sh
luse@luse-VirtualBox:~$ ./script1.sh
./script1.sh: line 4: ls.dat: Permission denied
./script1.sh: line 5: tree.dat: Permission denied
mkdir: cannot create directory `backups': Permissions denied
mv: failed to access '/home/backups': No such file or directory
./script1.sh: line 8: cd: backups: No such file or directory
mv: cannot stat 'ls.dat': No such file or directory
mv: cannot stat 'tree.dat': No such file or directory
luse@luse-VirtualBox:~$

screenshot of terminal showing error messages

1
задан 13 June 2020 в 00:31

1 ответ

You're doing a few things wrong.

  1. A user's home directory is /home/, not /home.
  2. You need a space between tree and the path.
  3. The specific mv command syntax you used to move the .txt files into the backups folder must come before cd backups.

Taking all these into account, your script should look like:

#!/bin/bash
cd ~
ls -al > ls.txt
tree ~ > tree.txt
mkdir backups
mv -t ~/backups ls.txt tree.txt
cd backups
mv ls.txt ls.bu
mv tree.txt tree.bu

Notes:

  1. ~ is the shorthand for /home/ for the current user, you can also use /home/ in place of ~ to specify the user.
  2. Since you're writing a script, I presume you would run it more than once. Running the script for the second time would throw an error unless you delete the backups folder before running the script again. To overcome this, you need to change the mkdir backups command to mkdir -p backups.
2
ответ дан 19 June 2020 в 21:25

Другие вопросы по тегам:

Похожие вопросы: