Home > Blockchain >  Create folders from txt file and if folders haven't been created write name ino error file
Create folders from txt file and if folders haven't been created write name ino error file

Time:10-31

I have been trying to create script which reads txt file(source.txt) and creating folders with this name and if folder somehow haven't been created I need to put name in Error.txt

Here I have script for windows which is working, but I need to create it for linux

Windows:

for /F "usebackq eol=| delims=" %G in ("source.txt") do md "Fld1\%~G" 2>nul & if not exist "Fld1\%~G">>"Error.txt"

Linux script throws error "syntax error near unexpected token done, without if [ ! -d Fld1/$line ] then echo "$line" > Error.txt script works correctly creating all I want, but I need this script with this if case.

while read line;
do
mkdir "Fld1/$line"
if [ ! -d Fld1/$line ]
then
echo "$line" > Error.txt
done < source.txt

I would be thankful for any solutions

CodePudding user response:

mkdir already tells you if it suceeded.

fi is missing.

I think you want to >> append to file.

Check your scripts with shellcheck . You script might look like:

while IFS= read -r line; do
   if ! mkdir "Fld1/$line"; then
      echo "Fld1/$line" >> Error.txt
   fi
done < source.txt

I think I would consider:

cd "Fld1" && xargs -d '\n' -n1 mkdir <source.txt 2>Error.txt
  • Related