使用阵列的两个不同的目录里面比较字符串 [英] Compare strings inside in the two different directories using array

查看:158
本文介绍了使用阵列的两个不同的目录里面比较字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这给code的场景。所有我想是比较被下面给出的文件。但是,在这个脚本空话发生。我认为这给code可以执行任何地方像/根,它将运行。请检查了这一点。

I don't get the scenario of this given code. All I wanted is to compare the files that is given below. But, in this script nothings happen. I assume that this given code can executed wherever like in /root and it will run. Please check this out.

#!/bin/bash
for file in /var/files/sub/old/*
do
# Strip path from file name
file="${file##*/}"

# Strip everything after the first hyphen
prefix="${file%%-*}-"

# Strip everything before the second-to-last dot
suffix="$(echo $file | awk -F. '{ print "."$(NF-1)"."$NF }')"

# Create new file name from $prefix and $suffix, and any version number
new=$(echo "/var/files/new/${prefix}"*"${suffix}")

# If file exists in the 'new' folder:
if test -f "${new}"
then
# Do string comparison to see if new file is lexicographically "greater than" old
if [[ "${new##*/}" > "${file}" ]]
then
  # If so, delete the old version.
  rm /var/sub/files/old/"${file}"
else
  # 'new' file is NOT newer, delete it instead.
  rm "${new}"
  fi
  fi
done

# Move all new files into the old folder.
mv /var/files/new/* /var/files/sub/old/

里面的例子文件的每个子目录。

Example files inside of each sub- directories ..

/var/files/sub/old/
firefox-24.5.0-1.el5_10.i386.rpm
firefox-24.5.0-1.el5_10.x86_64.rpm
google-1.6.0-openjdk-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm
google-1.6.0-openjdk-demo-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm

/var/files/new/
firefox-25.5.0-1.el5_10.i386.rpm
firefox-25.5.0-1.el5_10.x86_64.rpm
ie-1.6.0-openjdk-devel-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm
ie-1.6.0-openjdk-javadoc-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm
ie-1.6.0-openjdk-src-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm
google-2.6.0-openjdk-demo-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm

在这种情况下,我想获得是相同的文件。使是在给定的例子中相同的文件是:

In this instance, I want to get the files that are the same. So the files that are the same in the given example are:


  • 火狐-24.5.0-1.el5_10.i386.rpm

  • 火狐-24.5.0-1.el5_10.x86_64.rpm

  • 谷歌1.6.0-的OpenJDK-演示1.6.0.0-5.1.13.3.el5_10.x86_64.rpm

  • firefox-24.5.0-1.el5_10.i386.rpm
  • firefox-24.5.0-1.el5_10.x86_64.rpm
  • google-1.6.0-openjdk-demo-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm

老/ 目录和新/ 目录等同是:


  • 火狐-25.5.0-1.el5_10.i386.rpm

  • 火狐-25.5.0-1.el5_10.x86_64.rpm

  • 谷歌2.6.0-的OpenJDK-演示1.6.0.0-5.1.13.3.el5_10.x86_64.rpm

  • firefox-25.5.0-1.el5_10.i386.rpm
  • firefox-25.5.0-1.el5_10.x86_64.rpm
  • google-2.6.0-openjdk-demo-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm

该文件有他们的第一个字符的相似性。这将在终端显示。在此之后,将有另一个文件再次比较和比较结果为关于哪些文件是由数像文件的名称后更更新之一:火狐-24.5.0-1.el5_10。 i386.rpm 相比,火狐-25.5.0-1.el5_10.i386.rpm 。因此,在这种情况下的的firefox-24.5.0-1.el5_10.i386.rpm 将由取代火狐,25.5.0-1.el5_10 .i386.rpm ,因为它具有更大的价值,更更新之一相同类似的其他文件。如果旧的被删除,新会采取更换它。

The files have similarity for their first characters. It will display in the terminal. After that, there will be another comparing again of the files and the comparison is about which file is more updated one by the number after the name of the file like: firefox-24.5.0-1.el5_10.i386.rpm compared with firefox-25.5.0-1.el5_10.i386.rpm. So in that instance the firefox-24.5.0-1.el5_10.i386.rpm will be replaced by firefox-25.5.0-1.el5_10.i386.rpm because it has a greater value and more updated one and same as other files that are similar. And if the old one is removed and the new will take replacement of it.

所以,在这一刻脚本执行后的输出将是这个样子。

So at this moment after the script has been executed the output will be like this.

/var/files/sub/old/
google-1.6.0-openjdk-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm
firefox-25.5.0-1.el5_10.i386.rpm
firefox-25.5.0-1.el5_10.x86_64.rpm
ie-1.6.0-openjdk-devel-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm
ie-1.6.0-openjdk-javadoc-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm
ie-1.6.0-openjdk-src-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm
google-2.6.0-openjdk-demo-1.6.0.0-5.1.13.3.el5_10.x86_64.rpm

/var/files/new/
<<empty all files here  must to moved to other directory take as a replacement>>

谁能帮我做这个脚本?以上仅仅是一个例子。让我们假设有大量的文件,以作为相似,需要移除和移动。

Can anyone help me to make a script for this ? above is just an example. Let's assume that there are lots of files to considered as similar and need to removed and moved.

推荐答案

您可以使用来获取包的名称,而不版本或结构字符串:

You can use rpm to get the name of the package without version or architecture strings:

rpm -qi -p /firefox-25.5.0-1.el5_10.i386.rpm

给出:

Name        : firefox
Version     : 25.5.0
Release     : 1.el5_10
Architecture: i386
....

所以,你可以比较名称 s到找到相关的包。

这篇关于使用阵列的两个不同的目录里面比较字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆