Can someone please help with this because I can't seem to find a solution. I have the following script that works fine:
#!/bin/bash
#Checks the number of lines in the userdomains file
NUM=`awk 'END {print NR}' /etc/userdomains.hristian`;
echo $NUM
#Prints out a particular line from the file (should work with $NUM eventually)
USER=`sed -n 4p /etc/userdomains.hristian`
echo $USER
#Edits the output so that only the username is left
USER2=`echo $USER | awk '{print $NF}'`
echo $USER2
However, when I substitute the 4 on line 12 with the variable $NUM, like this, it doesn't work:
USER=`sed -n $NUMp /etc/userdomains.hristian`
I tried a number of different combinations of quotes and ${}, however none of them seem to work because I'm a BASH newbie. Help please :)
USER=`sed -n ${NUM}p /etc/userdomains.hristian`. As a side note: don't use upper case variable names in Bash! here you're doing it wrong:USERis very likely a variable set and used by your system; you might have a clash sooner or later! As a bonus note: don't use backticks, use$(...)instead:USER=$(sed -n ${NUM}p /etc/userdomains.hristian).tail -n1 /etc/userdomains.hristianwill do the job more efficiently.awk 'END { print $NF }' /etc/userdomains.hristian, although usingtail -n1 /etc/userdomains.hristian | awk '{ print $NF }'will be more efficient for large files.