gpt4 book ai didi

linux - Bash 到批量转换

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:05:23 26 4
gpt4 key购买 nike

在制作了一个创建目录的 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

任何输入将不胜感激

最佳答案

虽然我不了解 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”

关于linux - Bash 到批量转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14247671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com