I'm messing around with scripting, I am able to create a script which when run prompts me for a name for a new dir, creates it then creates several files, echoes lines out, then deletes it all.
What I would like to do is morph it slightly so it creates and names the directory all by itself!
Seems a pointless exercise I know, but messing around with it is the best way I learn.
Here is my current script:
#!/bin/bash
echo "Give a directory name to create:"
read NEW_DIR
ORIG_DIR=$(pwd)
[[ -d $NEW_DIR ]] && echo $NEW_DIR already exists, aborting && exit
mkdir $NEW_DIR
cd $NEW_DIR
pwd
for n in 9 8 7 6 5 4 3 2 1 0
do
touch file$n
done
ls file?
for names in file?
do
echo This file is named $names > $names
done
cat file?
cd $ORIG_DIR
rm -rf $NEW_DIR
echo "Goodbye"
9 8 7 6 5 4 3 2 1 0in your code can be shortened to{9..0}and you might as well replace the wholeforloop withtouch file{9..0}. – dessert Sep 04 '17 at 11:00pushdandpopdinstead of assigningpwdto variables. also, in your lineecho This file is namedyou should use>>, not>, if you want to get more than the last line. And maybemkdir -pcould help you to avoid the-dtest. (possibly the-pflag is not available everywhere) – ohno Sep 04 '17 at 13:16cd "${NEW_DIR}",[[ -d "${NEW_DIR}" ]]and> "${names}". This prevents potentially serious errors when variables contain spaces and certain other characters. (The curly brackets are optional except in rare cases.) – Paddy Landau Sep 06 '17 at 08:38echo -nprints your prompt without the trailing newline, so you get the cursor after the prompt. I think it looks nicer. – MDeBusk Jun 02 '22 at 23:19