I have the following script for creating projects:
clear
version="0.0.2"
echo "Project creator"
echo "---------------"
# ask user to specify a directory (could start with spelled-out home directory)
directoryName=""
# Input must meet the following criteria:
# 1.) No spaces at all in the directory path
# 2.) some characters must be entered
while [[ ${#directoryName} -eq 0 || $directoryName == *" "* ]]
do
echo -e "\nEnter the full directory of the project (no spaces):"
read directoryName
done
echo "You entered: $directoryName"
# uate directoryName so that the rest of the code can understand it
# create directory if it does not exist
eval mkdir "$directoryName"
# if successful
if [ $? -eq 0 ]
then
# call executable in /home/miwarren/CPPFileCreator to create a main.cpp file to put in the folder
$HOME/CPPFileCreator/a.out $directoryName
# copy a run.sh file into the folder, too...
eval cp $HOME/CPPFileCreator/run.sh $directoryName/run.sh
# you might want to put a fileCreator.sh in the folder as well
eval cp $HOME/fileCreator.sh $directoryName/fileCreator.sh
fi
I make sure to strip out any spaces (assumed that every unsafe string has at least a space in it, because injection-style attack). I want users to not have to spell out path parts when there exist variables for it (for example, $HOME
for the home directory).
Am I still good to use eval
here, and if not, what to do instead?
eval
? Try this:directoryName=foo;ls
, theneval mkdir "$directoryName"
. Then consider the effects of replacingls
with a script that does something likerm -rf /
. :) – Satō Katsura Sep 03 '16 at 05:39eval
? – Mike Warren Sep 03 '16 at 05:40mkdir -p "$directoryName"
?for d in "$directoryName"; do mkdir -p "$d"; done
? – Satō Katsura Sep 03 '16 at 05:41eval
, it shouldn't matter to this script. – CB Bailey Sep 03 '16 at 09:38