สคริปต์ที่ใช้ sed เพิ่ม“ e” ลงในไฟล์เอาต์พุต


18

ฉันมีสคริปต์ที่เพิ่มผู้ใช้ใหม่และสร้างโฮสต์เสมือนสำหรับชื่อโดเมนผู้ใช้ สคริปต์ทำงานได้อย่างยอดเยี่ยมโดยมีข้อยกเว้นหนึ่งข้อ ... ใน / etc / apache2 / sites-available / ไฟล์โฮสต์เสมือนทั้งหมดของฉันมีสำเนาสองชุดสำเนาที่มี e และอีกหนึ่งไฟล์ที่ไม่มี

ฉันเชื่อว่าปัญหาของฉันอยู่เมื่อฉันใช้คำสั่ง SED ฉันจะได้รับความคิดเห็นที่สอง?

นี่คือสคริปต์:

# set the working directory
dir=$(pwd)

# request the new domain name
printf "Enter the new domain name, without the www (i.e newdomain.com):\n"
read newdomain

# request the new username
printf "Enter the new username (i.e newusername):\n"
read username

# create the new user
sudo adduser $username

# copy the virtual host to sites-available
sudo cp /templates/domain-vhost /etc/apache2/sites-available/$newdomain

# echo results
echo "Successfully created the virtual host file at: /etc/apache2/sites-available/$newdomain.."

# change the domain name in the virtual host file
sudo sed -ie "s/NEWDOMAINNAME/$newdomain/" /etc/apache2/sites-available/$newdomain

# echo results
echo "Modified the virtual host to reflect the new domain: $newdomain.."

# change the directory path in the virtual host
sudo sed -ie "s/NEWUSERNAME/$username/" /etc/apache2/sites-available/$newdomain

# echo results
echo "Successfully modified the new virtual host file.."

# enable the site with apache
cd /etc/apache2/sites-available/
sudo a2ensite $newdomain

# echo results
echo "Successfully enabled the $newdomain.."

# change to previous working directory
cd $dir

# reload apache
sudo /etc/init.d/apache2 reload

# notify user of action
echo "Finished creating the new domain, $newdomain, and restarted Apache.."

3
ขออภัยหลังจากพิมพ์ว่าฉันรู้ข้อผิดพลาดของฉันเมื่อใช้คำสั่ง SED ฉันมีตัวเลือก -i เพื่อแก้ไขไฟล์ "inline" โดยไม่ต้องสร้างสำเนา ด้วยเหตุผลบางอย่างฉันเพิ่มคำสั่ง e ด้วยซึ่งควรจะเพิ่มสคริปต์ ฉันเปลี่ยน -i เป็น -i และสคริปต์ทำงานได้ตามต้องการ ฉันยังไม่สามารถตอบคำถามของตัวเองได้ดังนั้นฉันจึงเพิ่มความคิดเห็น
jason.dot.h

คำตอบ:


17

คุณต้องแยกจากกัน-iและ-eโต้แย้งเพื่อล่อลวง -ieกำลังบอกให้สร้างไฟล์สำรองข้อมูลโดยใช้ 'e' ต่อท้าย

การป้อนข้อมูลด้วยตนเองสำหรับ <code> -i </code>

หากต้องการแก้ไขเพียงแทนที่การร้องขอเพื่อขอ:

sudo sed -i -e "s/NEWUSERNAME/$username/" /etc/apache2/sites-available/$newdomain

แม้ว่าคุณจะได้รับความคิดเห็นเกี่ยวกับสิ่งนี้ แต่ฉันได้เพิ่มรายละเอียดไว้เป็นคำตอบเพื่อความสมบูรณ์
Jeremy Kerr

เมื่อฉันอ่านคำตอบนี้ครั้งแรกฉันเข้าใจสิ่งที่พูด แต่ไม่เข้าใจทันทีว่าทำไม e จึงถูกใช้เป็นคำต่อท้าย จากนั้นฉันตรวจสอบหน้าคู่มือและตระหนักว่าฉันเลือกอาร์กิวเมนต์ที่เป็นทางเลือก มันทำให้คำตอบนี้ชัดเจนในการหวนกลับ แต่ฉันใช้เวลาสักครู่เพื่อเข้าใจสาเหตุ (ด้วยเหตุนี้ภาพเพิ่ม)
Chris Schmitz
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.