Как вы сказали, xargs
не любит непарные двойные кавычки, если только вы не используете -0
, но -0
имеет смысл только в том случае, если вы подаете ему нулевые -завершающиеся данные. Итак, это не удается:
$ echo * | xargs
xargs: unmatched double quote; by default quotes are special to xargs unless you use the -0 option
name-with-backslash -name-with-dash-prefix
Но это работает:
$ printf '%s\0' -- * | xargs -0
-- name-with-backslash\ -name-with-dash-prefix name-with-double-quote" name-with-single-quote' name with space safe-name
В любом случае, ваш базовый подход — не лучший способ сделать это. Вместо того, чтобы возиться с xargs
и ls
и еще чем-то, просто используйте шелл-глобы:
$ for f in *; do ls -l -- "$f"; done
-rw-r--r-- 1 terdon terdon 4142 Aug 11 16:03 a
-rw-r--r-- 1 terdon terdon 0 Aug 11 15:34 'name-with-backslash\'
-rw-r--r-- 1 terdon terdon 0 Aug 11 15:34 -name-with-dash-prefix
-rw-r--r-- 1 terdon terdon 0 Aug 11 15:34 'name-with-double-quote"'
-rw-r--r-- 1 terdon terdon 0 Aug 11 15:34 "name-with-single-quote'"
-rw-r--r-- 1 terdon terdon 0 Aug 11 15:34 'name with space'
-rw-r--r-- 1 terdon terdon 0 Aug 11 15:34 safe-name
Вы можете обернуть свой скрипт (, который имеет права на выполнение на данный момент ), в формат пакета для рассматриваемой ОС.
Тогда ваши пользователи смогут просто установить пакет, и соответствующие права будут автоматически установлены.
debreate — простое решение этой самой проблемы в операционных системах на базе Debian -.
Простой способ — создать tarball, сжатый tar
архив. Если вы создаете его с привилегиями root, а пользователь извлекает содержимое также с доступом root, разрешения должны быть сохранены.
Примеры
sudo -cvzf filename.tar.gz directory # create a compressed tar archive of the directory and its content
cd /to-where-you-want-it-extracted
sudo -xvzf filename.tar.gz # extract the content from the archive
Подробности можно найти в man tar
, а в Интернете можно найти хорошие руководства по tar
.
Команда shar
, см. man shar
, была придумана именно для этого.
Из man shar
илиhttps://manpages.ubuntu.com/manpages/bionic/en/man1/shar.1.html
ОПИСАНИЕ
shar creates "shell archives" (or shar files) which are in text format and can be emailed.
These files may be unpacked later by executing them with /bin/sh. The resulting archive
is sent to standard out unless the -o option is given. A wide range of features provide
extensive flexibility in manufacturing shars and in specifying shar "smartness". Archives
may be fairly simple (--vanilla-operation) or essentially a mailable tar archive.
и
ПРИМЕРЫ
The first shows how to make a shell archive out of all C program sources. The second
produces a shell archive with all.c and.h files, which unpacks silently. The third
gives a shell archive of all uuencoded.arc files, into numbered files starting from
arc.sh.01. The last example gives a shell archive which will use only the file names at
unpack time.
shar *.c > cprog.shar
shar -Q *.[ch] > cprog.shar
shar -B -l28 -oarc.sh *.arc
shar -f /lcl/src/u*.c > u.sh