I am following a tutorial for Linux and I am a Windows user. I need to know, how can I execute code in Ubuntu terminal?

Here is what the exercise say:

The directory will now have 4 keys - private/public pairs of ZSK and KSK. We have to add the public keys which contain the DNSKEY record to the zone file. The following for loop will do this.

for key in `ls Kexample.com*.key`
echo "\$INCLUDE $key">> example.com.zone

Is there a way to execute this code from terminal?

EDIT: Following the answer from @Karl I get permission denied. I use sudo in normal commands like mv but how to deal with this in multiple lines and commands? here is my code listing:

x@mypc:/var/cache/bind$ for key in `ls Kcom*.key`
> do
> echo "\$INCLUDE $key" >> com.zone
> done
bash: com.zone: Permission denied
bash: com.zone: Permission denied
  • 3
    That's a bad tutorial if it tells you to iterate over the output of ls. Use for key in Kexample.com*.key instead. – choroba May 23 '18 at 22:48

You can type it at the bash prompt (command-line input, or CLI) and hit after each line. Bash will keep track of it and send you to the next line where it will wait for your input after a > character as the prompt rather than a dollar or pound sign.

for key in $(ls Kexample.com*.key) <ENTER>
> do <ENTER>
> echo "\$INCLUDE $key">> example.com.zone <ENTER>
> done <ENTER>

After you press enter on the last line you'll get the output the command has available to it. Now then, some explanation:

  • key is the variable that will be populated. That's why subsequent uses put a $-sign in front of it ($key)
  • The use of the \-character is escaping what comes next, as in \$ so $INCLUDE isn't interpreted as a variable.
  • >> means to append to the end of the file, example.come.zone
  • <ENTER> is my way of telling you to press the return key.

I hope this helps. Now if you want to put it in a file and execute it as a command, there are a lot more things to consider, such as your $PATH variable, etc, etc.

Good luck with this new learning experience. I've been using Linux as my primary desktop OS for over 22 years now. Love it!

  • Thanks. Could you please look at my edit and inform me how to deal with permission denied output? – None May 23 '18 at 23:01
  • Before executing this script, become root. There are a few ways to do this - if you are used to sudo, the easiest is to type "sudo /bin/bash" which will launch a new bash shell as root, thus giving you elevatated privileges until you exit it. – davidgo May 24 '18 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.