繁体   English   中英

Bash-名称不断变化时如何搜索文件

[英]Bash- How to search for a file when the name keeps changing

我在bash中的代码有问题。

我希望我的代码检查目录中是否存在文件,但是由于该文件将不断更改他的名字,因此我想搜索他的“扩展名”。

因此,基本上,当我启动脚本时,它将检查目录是否存在,如果确实存在,它将创建目录,然后我要检查是否有任何以“。”开头的文件。 (点)在该目录中(如果存在的话)将创建一个名为“ .200”的空文件

这是代码。

#!/bin/bash

directory=$HOME/.impressora
mkdir -p $directory
if [ ! -f "$directory/.*" ]
then echo "" > $directory/.200

问题是,此后,我添加了一堆代码,这些代码将不断更改该文件的名称,例如,它将从“ .200”更改为“ .199”,再更改为“ .198”更改为“ .197”等...

因此,在我的“ If”语句中,我必须搜索目录中是否有任何以“。”开头的文件。 (点)。

我已经使用命令“ shopt -s dotglob”和“ shopt -u dotglob”使他忽略“”。 和“ ..”文件,默认情况下放在该目录中。

我试图通过添加一个名为“ file”的变量来进行测试,并尝试使其具有“ .200”文件扩展名的值,然后进行打印,但是存储在该变量中的值为“。*”而不是“ 200”是我想要的...这是带有变量的代码。

Directory=$HOME/.impressora
file=$HOME/.impressora/.*
echo "$file"
mkdir -p $directory
if [ ! -f "$directory/.*" ]
then echo "" > $directory/.200
fi

希望您能帮帮我,我一直在拼命解决这个问题。谢谢。

您可以将find命令传递给grep ,它可以使用正则表达式。 此命令将告诉您目录中有多少文件以句点开头,并且至少包含一个数字(不包含字母):

find testing/  -printf "%f\n" | grep -c '\.[0-9]\+$'

然后,您可以检查grep结果以查看其是否大于0。

(取决于您的“ find”版本,您也许还可以直接在find命令中使用正则表达式。另一个警告:需要转义+而不转义$ ,这取决于您的操作系统。)

您可以在首次创建文件时记录它的索引节点。 重命名文件后,索引节点将不会更改:

#!/bin/bash

directory=$HOME/.impressora
if [ ! -d "$directory" ]; then
    # create the directory and create the initial file
    mkdir -p $directory
    touch $directory/.200

    # record the file's inode
    stat --format=%i $directory/.200 > $directory/.inode
fi

# retrieve the file's path using its inode value
filepath=$(find $directory -inum $(cat $directory/.inode))
echo "\$filepath: $filepath"

尝试重命名$directory/.200并再次运行此脚本,以验证可以使用其inode找到该文件。

因此,我一直试图理解并尝试使用你们给我的解决方案,但是我告诉过您,我真的不擅长使用Shell脚本,因此我无法实施你们所给我的解决方案。 因此,让我尝试将代码分解为简单的人类语言,希望您可以帮助我将其转换为外壳语言。 我会注释每一行代码,不是因为我不希望您理解它的作用,而是因为我认为它将做的事情可能与xD的实际工作有所不同。 我不知道我的英语是否正确,但是对不起,我很抱歉。

所以就到这里。

!#/bin/bash

directory=$HOME/.impressora
# stores the path $HOME/.impressora in the variable directory

cred=0

mkdir -p $directory
# check if the path $HOME/.impressora exists, if it doesen't create it

if [ ! -f "$directory/.*" ]
#now at this part i want the script to create a file called ".200" if 
#there is no other file started by a "." (dot) in that directory
#i cannot put a specific file name because when i run this script for the
#first time on a fresh computer i want it to create a .200 file wich,
#afterwards will be renamed over and over again

then echo "" > $directory/.200
# so if there is no file started by a "." (dot) on that directory,
#create a file called ".200"
fi

creditos=$(echo $directory/.* | cut -c7-)
#now i want to set the variable creditos = to the name of the file in the
#directory we just created but without the "." (dot).
#so the first time this program runs i want creditos to be = to 200
#but then when i change the name of the file to ".199" i want the variable
#creditos to be = to 199 and so on...

if [ $1 = "lp" ]
#now this script is called "project" so if i type "./project lp" 
#in the terminal do...

then
if [ -d $directory ] && [ $creditos > 0 ]
#if the directory exists and the variable "creditos" is grater than 0...

then
    echo "" > $directory/lp-$imp
    #create an empty file called "lp-the value of a varable wich needs to
    #increment by 1 each time i run the command "./project lp"
    #this is another thing i dont know how to do, how do i implement a
    #variable in wich her value is persistent?
    #the objective is, each time i type "./project lp" in the console
    #if the directory exists and the variable creditos is greater than 0
    #create a file called "lp-1", "lp-2" , "lp-3" and so on

    $cred=$creditos
    # on the first usage of the script "$cred" = 200
    ((cred--))

    mv $directory/.$creditos $directory/.$cred
    #this is my way to rename the file, so it would go from ".200"
    #to ".199" and so on
else
    echo "Diretorio inexistente ou creditos insuficientes"
    #if the directory doesent exist or the variable creditos
    #is lower than 200, print that message
fi
fi

对不起,那堆文字,但是我没有更好的方法来准确解释我的需求,希望您能帮帮我! 非常感谢您的宝贵时间!

由于该文件没有名称(因为它以点开头),因此它自动是第一个出现在目录中的文件。 因此,首先您需要将目录“ ls -a ”。 接下来,您需要跳过出现的两个“文件”:

..

因此,您执行“ head -3 ”命令,以便可以选择文件夹的前三行(文件)。 接下来,您需要选择这3个中的最后一个,以便再次对其进行管道处理并执行“ tail -1

最后,您需要删除.200文件中的点,以便仅将值保存到var中,为此,您必须通过管道传递“ cut -f2 -d”。

基本上,您需要更换

creditos=$(echo $directory/.* | cut -c7-)

creditos=$(ls -a $directory | head -3 | tail -1 | cut -f2 -d".")

暂无
暂无

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

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