For the life of me, I cannot figure out how to chroot through a script, and assign a value to a variable. Okay, so I learned how to chroot and execute commands from a script without halting the flow of the script:
#!/bin/bash
# Mini script for chrooting into chroot_dir and executing commands.
chroot_dir=$1
cat << EOF | chroot $chroot_dir /bin/bash
sysname=\`hostname\`
echo "You are running script.sh on ${sysname}"
EOF
Running this script, ./script.sh "/chroot_dir/"
, I get the following output:
You are running script.sh on
It seems like either sysname never gets assigned, or sysname loses its value immediately. How do I assign a value to sysname using this method to chroot?