1

在制作了一个创建目录的 bash 文件,然后将文件从一个目录传输到每个创建的目录之后,我很好奇如何在批处理文件中执行此操作。

这是 bash 代码:

#!/bin/bash
# For each item in file named in $1, make a directory with this name.
#   and copy all files named in file $2 from templates folder to new directory

for user in `cat $1`
do
  if [ -d $user ]
  then 
    echo Directory $user already exists
    rm -r $user
   echo $user has been deleted
  fi   

  mkdir $user
    echo Directory $user created


  for file in `cat $2`
  do
    cp /home/student/Desktop/OS/templates/$file $user
    chmod 700 $user/$file
  done
  echo Directory for $user set up
done

任何投入将不胜感激

4

1 回答 1

1

虽然我不了解 Bash,但我认为我的 Batch 版本的程序是精确的:

@echo off
rem For each line in file named in %1, make a directory with this name.
rem   and copy all files named in file %2 from templates folder to new directory
for /F "delims=" %%u in (%1) do (
   if exist "%%u" (
      echo Directory %%u already exists
      rd /S /Q "%%u"
      echo %%u has been deleted
   )
   md "%%u"
   echo Directory %%u created
   for /F "delims=" %%f in (%2) do (
      copy "/home/student/Desktop/OS/templates/%%f" "%%u"
      rem chmod not exist in Batch
   )
)

但是,我会修改以前的程序,以便只读取一次模板文件并将其行存储在一个数组中:

@echo off
rem For each line in file named in %1, make a directory with this name.
rem   and copy all files named in file %2 from templates folder to new directory
setlocal EnableDelayedExpansion
set temp=0
for /F "delims=" %%f in (%2) do (
   set /A temp+=1
   set template[!temp!]=%%f
)
for /F "delims=" %%u in (%1) do (
   if exist "%%u" (
      echo Directory %%u already exists
      rd /S /Q "%%u"
      echo %%u has been deleted
   )
   md "%%u"
   echo Directory %%u created
   for /L %%i in (1,1,%temp%) do (
      copy "/home/student/Desktop/OS/templates/!template[%%i]!" "%%u"
      rem chmod not exist in Batch
   )
)

安东尼奥又名“Aacini”

于 2013-01-09T23:33:55.213 回答