chroot
作業ディレクトリを設定できるコマンドのラッパーを作成したいとします。そうでなければ、chroot
意味体系が維持される。したがって、意味は次のようになります。
chroot.sh <chroot-dir> <working-dir> <command> [arg]...
私の無邪気な試みは次のとおりです
#!/bin/sh
chroot_dir=$1
working_dir=$2
shift 2
chroot "$chroot_dir" sh -c "cd $working_dir; $*"
ただし、これは正しく処理されません。
chroot.sh /path/to/chroot /tmp touch 'filename with space'
正しく実装する方法がわかりません。 bashしか使えませんか?
私のCentOS 6システムでは、コマンドはchroot
作業ディレクトリの設定をサポートしていません。他のシステムではそうではないかもしれません。
ベストアンサー1
これを処理する方法は、パラメータを参照リストに渡すことです。後ろに以下で呼び出されるコマンドchroot
:
chroot "$chroot_dir" sh -c 'cd "$1" && shift && "$@"' - "$working_dir" "$@"
以下は、一般的な環境設定を含む実際の例ですchroot
。
設定
# Where
chroot="$HOME/chroot"
# Directory structure
mkdir -p -m755 "$chroot"/{bin,dev,lib,tmp}
chmod 1777 "$chroot"/tmp
# Shell
cp -a /bin/dash "$chroot"/bin/sh
# Devices
cp -a /dev/{null,tty} "$chroot"/dev
# Libraries
cp -p $(ldd /bin/dash | sed 's/^.*=> //' | awk '!/vdso/ {print $1}' ) "$chroot"/lib/
# Demonstration executable
cat >"$chroot"/bin/args <<'EOF'
#!/bin/sh
#
echo "Got $# arg(s): $*"
echo "CWD: $(pwd)"
printf '> %s <\n' "$@"
EOF
chmod a+x "$chroot"/bin/args
実行(アプリケーションargs
)
working_dir=/tmp
chroot "$chroot" /bin/sh -c 'cd "$1" && shift && "$@"' - "$working_dir" args 'one two' three
出力
Got 2 arg(s): one two three
CWD: /tmp
> one two <
> three <
出力が示すように、スペースとパラメータは正しく保持されます。