简体   繁体   English

Bash脚本文件的创建和填充

[英]Bash-script File creation and fill

I am trying to write a bash-script which will create a certain number of files which are passed into an argument, and also fill that file with relevant information. 我正在尝试编写一个bash脚本,该脚本将创建一定数量的文件并传递给参数,并用相关信息填充该文件。 However, for some reason I can't get it to work properly, and googling turned up nothing. 但是,由于某种原因,我无法使其正常工作,并且使用Google谷歌搜索后什么也没发现。

I want it to create files like the following: FileName: 我希望它创建如下文件:FileName:

Prob_3_ch_17.cpp

Filled as the following: 填写如下:

/*
User: Johnny Smith
Prob: 3
Output: 
*/

Where the first command line argument is the chapter number. 其中第一个命令行参数是章节编号。 The second is the starting number, and the third is the ending number. 第二个是起始编号,第三个是结束编号。 So if the following is ran 所以如果执行以下

sh ../FileMaker.sh 17 1 10

It will make 10 files, each filled with the appropriate data and proper file names, for chapter 17. This is the script that I came up with. 对于第17章,它将生成10个文件,每个文件都填充有适当的数据和正确的文件名。这是我想到的脚本。

#!/bin/bash

# Syntax:
# $1 = Chapter Number
# $2 = Starting Number
# $3 = Ending Number

CHAPTER=$1
FIRST=$2
LAST=$3
U_NAME="Johnny Smith"

# Name: Prob_$NUMBER_Ch_$1.cpp

for ((NUMBER=$FIRST; $NUMBER<=$LAST; $NUMBER++));
   do
      if [ -e "Prob_$NUMBER_Ch_$CHAPTER.cpp" ]; then
         echo "File Prob_$NUMBER_Ch_$CHAPTER.cpp already exists!"
      else
         echo "/* \n User: $U_NAME \n Problem: $NUMBER \n Output: \n */" >> "Prob_$NUMBER_Ch_$CHAPTER.cpp"
   done

However, it doesn't run. 但是,它没有运行。 I get the following error: 我收到以下错误:

../FileMaker.sh: line 21: syntax error near unexpected token `done'
../FileMaker.sh: line 21: `done'

I've googled and found other people have had the same problem, but I didn't fully understand what was meant in the solution. 我搜寻了一下,发现其他人也遇到了同样的问题,但是我不完全了解解决方案的含义。 Can someone please help me? 有人可以帮帮我吗? I'm not the best at shell scripting, and I'm trying to learn by making scripts like this. 我不是最擅长的Shell脚本编写人员,而且我正在尝试通过编写此类脚本来学习。

Thanks in advanced. 提前致谢。

Syntax for if block and for-loop is wrong. if块和for循环的语法错误。 correct syntax would be - 正确的语法是-

    for ((NUMBER=FIRST; NUMBER<=LAST; NUMBER++));
   do
      if [ -e "Prob_$NUMBER_Ch_$CHAPTER.cpp" ]; then
         echo "File Prob_$NUMBER_Ch_$CHAPTER.cpp already exists!"
      else
         echo "/* \n User: $U_NAME \n Problem: $NUMBER \n Output: \n */" >> "Prob_$NUMBER_Ch_$CHAPTER.cpp"
      fi
   done

See example 11.12 for c style for loop. 有关循环的c样式,请参见示例11.12

To create files use "Prob_"$NUMBER" Ch "$CHAPTER".cpp" at all the places. 要创建文件,请在所有位置使用“ Prob _” $ NUMBER“ Ch ” $ CHAPTER“ .cpp”。

Most shells allow a set -x at the top. 大多数外壳在顶部都允许使用set -x This show you tracing of the script as it executes. 这显示了脚本执行时的跟踪。

  1. You need a trailing fi to close the if statement. 您需要结尾的fi来关闭if语句。
  2. Shell math need NUMBER=$(expr $NUMBER + 1) , it doesn't understand $NUMBER++ . Shell数学需要NUMBER=$(expr $NUMBER + 1) ,它不理解$NUMBER++
  3. You might find the printf command more handy than echo . 您可能会发现printf命令比echo更方便。

You will still have your variable use wrong in the loop and are only creating one file. 您仍然会在循环中使变量使用错误,并且仅创建一个文件。 I hope you can figure that out as this seems like homework. 我希望您能弄清楚这似乎是家庭作业。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM