1

Background

I am trying to use heredoc to write data to a file using a bash script.

Issue

When running the below shell script, I get the error:

line 11: /etc/hostapd/hostapd.conf: Permission denied

This error is referencing the line: cat <<- EOF > "$FILE"

How do I cat $FILE using sudo privileges?

Code

#!/bin/bash

FILE="/etc/hostapd/hostapd.conf"

SSID="Test Access Point"
PASSWORD="12345"

[[ -e "$FILE" ]] && \
    sudo touch "$FILE"

cat <<- EOF > "$FILE"
    interface=wlan0
    driver=nl80211
    ssid="$SSID"
    hw_mode=g
    channel=6
    wmm_enabled=0
    macaddr_acl=0
    auth_algs=1
    ignore_broadcast_ssid=0
    wpa=2
    wpa_passphrase="$PASSWORD"
    wpa_key_mgmt=WPA2-PSK
    wpa_pairwise=TKIP
    rsn_pairwise=CCMP
EOF
Nicholas Adamou
  • 711
  • 10
  • 23

0 Answers0