首页 \ 问答 \ 基本的bash编程(Basic bash programming)

基本的bash编程(Basic bash programming)

如何在bash中编写函数来执行以下任务:

  • 计算名为myDir的目录中.pdf文件的数量。

  • 打印一个名为data.txt的文件的第n行,其中n是一个存储整数的变量。

  • 运行脚本userinput.o,该脚本需要来自STDIN的数据。 但是,您必须从文件input.txt而不是从终端提供数据。

  • 比较两个文件foo1.txt和foo2.txt的内容。 如果文件匹配,请打印“文件是同义的。”。 如果不是,则打印“文件不同”。 。 取消所有其他输出。

  • 使用gcc编译器编译文件windows8.c,并将STDERR和STDOUT重定向到compile.out文件。

这是我的代码:

function part_a 
{               


    return                      
}                               

function part_b
{              

    return                     
}                              

function part_c
{
    "input.txt">>"userinput.o"
    return
}

function part_d
{
    diff foo1.txt foo2.txt
    if (( $? != 0 ))
    then
        echo " Files are different. "
    else
        echo " Files are similar. " 
        return
}

function part_e
{
    gcc "windows8.c" -o quick_sim
    make &> compile.out
    return
}

# To test your function, you can call it below like this:

part_c
part_d

谢谢 :)


How do I write functions in bash to perform the following tasks:

  • Count the number of .pdf files in a directory called myDir.

  • Print out the n-th line of a file called data.txt, where n is a variable that stores an integer.

  • Run the script userinput.o , which expects data from STDIN. However, you must provide data from a file input.txt instead of from the terminal.

  • Compare the contents of two files foo1.txt and foo2.txt. If the files match, print"Files are simliar.". If not,print"Files are different." . Suppress all other output.

  • Compile the file windows8.c with the gcc compiler and redirect both STDERR and STDOUT to the file compile.out.

This is code I have for it:

function part_a 
{               


    return                      
}                               

function part_b
{              

    return                     
}                              

function part_c
{
    "input.txt">>"userinput.o"
    return
}

function part_d
{
    diff foo1.txt foo2.txt
    if (( $? != 0 ))
    then
        echo " Files are different. "
    else
        echo " Files are similar. " 
        return
}

function part_e
{
    gcc "windows8.c" -o quick_sim
    make &> compile.out
    return
}

# To test your function, you can call it below like this:

part_c
part_d

thanks :)


原文:https://stackoverflow.com/questions/42242389
更新时间:2021-11-23 09:11

最满意答案

我发现任何时候更新项目的POM都有助于重新运行m2eclipse,否则它不会看到新的外部依赖关系。

如果您在添加新的依赖关系(JPA + Hibernate或其他任何)到您的POM之后还没有尝试运行此操作,请尝试一下,看看它是否有效。

mvn clean install eclipse:clean eclipse:eclipse eclipse:m2eclipse

请注意,执行此操作后,您可能需要刷新您的Eclipse项目以使更改生效。

它可能不需要所有这些论据都起作用,但我只是为了确定而在一点上添加了它们。 您可以调整参数列表以使其适用于您。

这样做可能会让您的项目中的某些Eclipse偏好失效,因此希望您拥有在POM中重建项目所需的一切。 例如,我在其中一个POM中添加了一些额外的东西,以确保当使用maven重新构建eclipse项目时,springnature和springbuilder将添加到我的eclipse项目中。

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-eclipse-plugin</artifactId>
      <configuration>
        <additionalProjectnatures>
          <projectnature>
            org.springframework.ide.eclipse.core.springnature
          </projectnature>
          <projectnature>
            org.eclipse.wst.common.project.facet.core.nature
          </projectnature>
          <projectnature>
            org.eclipse.wst.common.modulecore.ModuleCoreNature
          </projectnature>
          <projectnature>
            org.eclipse.jem.workbench.JavaEMFNature
          </projectnature>
        </additionalProjectnatures>
        <additionalBuildcommands>
          <buildcommand>
            org.eclipse.wst.common.project.facet.core.builder
          </buildcommand>
          <buildcommand>
            org.eclipse.wst.validation.validationbuilder
          </buildcommand>
          <buildcommand>
            org.springframework.ide.eclipse.core.springbuilder
          </buildcommand>
        </additionalBuildcommands>
        <wtpversion>1.5</wtpversion>
      </configuration>
    </plugin>
  </plugins>
</build>

最后一点被添加为警告! 如果他们没有准备好运行eclipse:*命令的后果,会痛恨某人采用正在运行的Eclipse项目并销毁一堆配置。


I have found that any time you update a project's POM it helps to rerun m2eclipse or it will not see the new external dependencies.

If you have not tried to run this since you added your new dependencies ( JPA + Hibernate or whatever ) to your POM then give it a shot and see if it works.

mvn clean install eclipse:clean eclipse:eclipse eclipse:m2eclipse

Please note that after you execute this, you will probably need to refresh your Eclipse projects for the changes to take effect.

It might not require all of these arguments to work, but I sort of added them all at one point just to make certain. You can tweak the list of arguments to get it to the point that it works for you.

Doing this may blow away some of your Eclipse preferences for your project, so hopefully you have everything needed to rebuild your project already in your POM. For instance, I have a bunch of extra things in one of my POMs to ensure that things like springnature and springbuilder are added to my eclipse project when maven is used to rebuild the eclipse project.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-eclipse-plugin</artifactId>
      <configuration>
        <additionalProjectnatures>
          <projectnature>
            org.springframework.ide.eclipse.core.springnature
          </projectnature>
          <projectnature>
            org.eclipse.wst.common.project.facet.core.nature
          </projectnature>
          <projectnature>
            org.eclipse.wst.common.modulecore.ModuleCoreNature
          </projectnature>
          <projectnature>
            org.eclipse.jem.workbench.JavaEMFNature
          </projectnature>
        </additionalProjectnatures>
        <additionalBuildcommands>
          <buildcommand>
            org.eclipse.wst.common.project.facet.core.builder
          </buildcommand>
          <buildcommand>
            org.eclipse.wst.validation.validationbuilder
          </buildcommand>
          <buildcommand>
            org.springframework.ide.eclipse.core.springbuilder
          </buildcommand>
        </additionalBuildcommands>
        <wtpversion>1.5</wtpversion>
      </configuration>
    </plugin>
  </plugins>
</build>

This last bit was added as a warning! Would hate for someone to take a working Eclipse project and destroy a bunch of the configuration if they were not already prepared for the consequences of running the eclipse:* commands.

相关问答

更多
  • 如果将项目转换为maven项目,将自动创建pom.xml ,如果没有 1-创建pom.xml文件 - 单击项目目录 - > new-> file 2-搜索frm google maven依赖项ex:如果你想下载spring-context.jar,然后搜索maven spring context,你将获得groupid和artifactid将它粘贴到pom.xml .jars将自动下载。 If you convert your project to maven project pom.xml will be ...
  • 检查maven依赖项是否包含在类路径中,如果不存在则添加。 Right click on project -> Properties -> Build Path -> Libraries -> Add Library -> Maven Managed Dependencies 之后你可能还需要 Select Project -> From top menu -> Project -> Clean and build if auto build is not enabled Actually the p ...
  • 问题解决了! 我不知道究竟解决了什么,但是我在Eclipse中做了4件事情: 窗口 - >首选项:Maven->安装:全局设置 - >打开文件和硬编码的localRepository 项目 - >清除 右键单击项目:Maven->更新依赖项 右键单击项目:Maven->更新项目配置 我想这是Update依赖关系,因为在前两次之后没有变化。 Problem solved! I don't know what exactly solved it, but I did 4 things in Eclipse: W ...
  • 项目方面允许IDE更好地理解您的项目,以便它可以为您执行一些特殊操作。 例如,应用动态Web模块会使eclipse知道您的Project将在Web Server上运行,因此您需要将该项目添加到Web服务器。 在将Dynamic Web Module应用于项目时,eclipse还假定在运行时向您提供Web服务器依赖项。 Facets保持在IDE级别。 虽然maven依赖项为您提供了项目执行任务所需的库。 部署后,它们会与您的项目包保持一致。 Project facets allows IDE to under ...
  • 我发现任何时候更新项目的POM都有助于重新运行m2eclipse,否则它不会看到新的外部依赖关系。 如果您在添加新的依赖关系(JPA + Hibernate或其他任何)到您的POM之后还没有尝试运行此操作,请尝试一下,看看它是否有效。 mvn clean install eclipse:clean eclipse:eclipse eclipse:m2eclipse 请注意,执行此操作后,您可能需要刷新您的Eclipse项目以使更改生效。 它可能不需要所有这些论据都起作用,但我只是为了确定而在一点上添加了它 ...
  • eclipse:eclipse -Dwtpversion = 2.0(这有效) eclipse:eclipse -Dwtpversion=2.0 (this works)
  • 在HTTPS站点上下载时,需要在信任库中添加服务器证书。 然后,在运行maven命令时,不要忘记为此信任库添加环境属性。 以下是有关如何创建信任库的链接: 数字证书:如何将.cer文件导入到.truststore文件中? 而且: -Djavax.net.ssl.trustStore = /家庭/路径/到/信任 在运行编译时使用信任库。 您也可以按照Maven的指南操作: http://maven.apache.org/guides/mini/guide-repository-ssl.html As you ...
  • 动态Web项目仅通过手动将Jars放在WEB-INF / libs文件夹中来解析依赖关系。 没有其他办法。 因此,您需要手动将Dynamic Web Project的从属jar放入libs文件夹中。 不要忘记在添加后右键单击Lib中的Jars - >添加到构建路径 这就是为什么我们这些天使用maven而不是旧的Dynamic Web Projects。 Dynamic Web Projects resolves dependency only by manually placing the Jars in ...
  • 首先:不要使用maven-eclipse-plugin和m2e / m2eclipse,两者不兼容。 m2e应该能够导入你的Maven项目并按照Eclipse的预期进行设置。 您是否为m2e安装了WTP附加组件? 这篇博文可能值得一读。 如果您的任何pom.xml文件调用任何特定插件(例如,用于代码生成),您可能需要相应的m2e插件。 这篇wiki帖子可能会让你知道为什么。 您可能考虑的另一个尝试是设置基于Helios的环境。 但是,检查您可能需要的所有插件是否仍然可用。 希望其中一些会有所帮助。 Firs ...
  • 如果您不想在项目B中使用maven,那么您可以通过项目A中的maven将生成的库复制到项目B,执行以下操作: 在项目A中,在Eclips中打开项目,右键单击项目A - > Maven - >更新项目,然后在项目A中,打开Java资源 - >库 - > Maven Dependecies,然后选择此文件夹中的所有库(按住shift然后单击第一个图书馆和最后一个图书馆)。 右键单击然后复制。 在项目B中,创建一个新的lib文件夹并将其过去到所有这些库中。 然后右键单击项目B - >属性 - >选择Java构建路 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。