1

I use SCP a lot to transfer log files from servers to a jumpbox where I can analyse and troubleshoot etc. If I have a cluster of servers and I want to create a set of subdirectories I do it like this:

mkdir -p /foo/bar-nnn/{mailserver,dnsserver,minecraftserver,syslogserver}

Lets's say 'bar-nnn' is a reference of sorts; be that a ticket number or incident etc. What I want to be able to do is run a script or a shell command which will prompt me for what 'bar-nnn' should be then go and create all the subfolders required.

I'm pretty sure I'm going to need a for loop but can't quite get my head around it.

1 Answers1

2

Try this:

IFS= read -r -p "Folder name: " dir
mkdir -p "/foo/${dir}/"{mailserver,dnsserver,minecraftserver,syslogserver}
Artur Szymczak
  • 1,933
  • 12
  • 12
  • Why ${dir} instead of "$dir", or is it the same? – FelixJN Feb 17 '16 at 13:45
  • That's done it! I used this to make it a oneliner:

    read -p "Folder name: " dir; mkdir -p /foo/${dir}/{mailserver,dnsserver,minecraftserver,syslogserver}

    – Moif Murphy Feb 17 '16 at 13:45
  • @Fiximan ${dir} and $dir is the same, but ${variable} convention is newer. – Artur Szymczak Feb 17 '16 at 13:47
  • @ArturSzymczak I see, I thought you had included arrays. – FelixJN Feb 17 '16 at 13:51
  • @Fiximan, in this example they are the same, but when ${dir}ectory vs $directory is where the distinction really makes a difference. The former being the value of $dir ending with 'ectory', the latter being the value of $directory. – Devon Feb 17 '16 at 15:35
  • @Devon I used to use double quotes for these cases - but the curly brackets seem to be nicer in terms of readability - and I am far from using spaces in my vars... – FelixJN Feb 17 '16 at 15:41
  • @Fiximan The double quotes are necessary, the braces rarely are. See http://unix.stackexchange.com/questions/4899/var-vs-var-and-to-quote-or-not-to-quote and our many [tag:quoting] questions. – Gilles 'SO- stop being evil' Feb 17 '16 at 23:03