简体   繁体   English

如何编写一个带有一个参数的bash shell脚本(目录名)

[英]How to write a bash shell script which takes one argument (directory name)

How to write a bash shell script called 'abc' which takes one argument, the name of a directory, and adds the extension ".xyz" to all visible files in the directory that don't already have it 如何编写一个名为'abc'的bash shell脚本,它接受一个参数,一个目录的名称,并将扩展名“.xyz”添加到目录中尚未拥有它的所有可见文件中

I have mostly written the code which changes the filenames inside the current directory but I can't get the script to accept an argument (directory name) and change the filenames of that directory 我大部分编写了更改当前目录中的文件名的代码,但是我无法让脚本接受参数(目录名)并更改该目录的文件名

#!/bin/bash
case $# in
0) echo "No directory name provided" >&2 ; exit 1;;
1) cd "${1}" || exit $?;;
*) echo "Too many parameters provided" >&2 ; exit 1;;
esac

for filename in *
do
echo $filename | grep "\.xyz$"
if [ "$?" -ne "0" ]
then mv "$filename" "$filename.old"        
fi
done

additional instructions include; 附加说明包括; Within 'abc', use a "for" control structure to loop through all the non-hidden filenames in the directory name in $1. 在'abc'中,使用“for”控制结构遍历$ 1中目录名中的所有非隐藏文件名。 Also, use command substitution with "ls $1" instead of an ambiguous filename, or you'll descend into subdirectories. 此外,使用命令替换“ls $ 1”而不是模糊文件名,或者您将进入子目录。

EDIT: The top part of the question has been answered below, however the second part requires me to modify my own code according to the following instructions: 编辑:问题的上半部分已在下面得到解答,但第二部分要求我根据以下说明修改我自己的代码:

Modify the command substitution that's being used to create the loop values that will be placed into the "filename" variable. 修改用于创建将放入“filename”变量的循环值的命令替换。 Instead of just an "ls $1", pipe the output into a "grep". 而不只是“ls $ 1”,将输出管道输入“grep”。 The "grep" will search for all filenames that DO NOT end in ".xyz". “grep”将搜索所有不以“.xyz”结尾的文件名。 This can easily be done with the "grep -v" option. 这可以通过“grep -v”选项轻松完成。 With this approach, you can get rid of the "echo ... | grep ..." and the "if" control structure inside the loop, and simply do the rename. 使用这种方法,你可以摆脱循环中的“echo ... | grep ...”和“if”控制结构,并简单地进行重命名。

How would I go about achieving this because according to my understanding, the answer below is already only searching through filenames without the .xyz extension however it is not being accepted. 我将如何实现这一目标,因为根据我的理解,下面的答案只是在没有.xyz扩展名的情况下搜索文件名,但是它没有被接受。

Your description is a little unclear in places, so I've gone with the most obvious: 你的描述在某些地方有点不清楚,所以我最明显的是:

#!/bin/bash

# validate input parameters
case $# in
    0) echo "No directory name provided" >&2 ; exit 1;;
    1) cd "${1}" || exit $?;;
    *) echo "Too many parameters provided" >&2 ; exit 1;;
esac

shopt -s extglob # Enables extended globbing

# Search for files that do not end with .xyz and rename them (needs extended globbing for 'not match')
for filename in !(*.xyz)
do
    test -f && mv "${filename}" "${filename}.xyz"
done

The answer to the second part is this: 第二部分的答案是:

#!/bin/bash
for file in $(ls -1 "$1" | grep -v '\.old$'); do
mv "$file" "$file.old"
done

I got it from somewhere 我是从某个地方得到的

暂无
暂无

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

相关问题 如何为 bash 编写脚本来归档目录并保留其名称? - How to write a script for bash to archive a directory and keep its name? 循环遍历目录一层,并在该目录中以 bash 中的目录名称作为参数执行脚本 - Loop over directories one level deep and execute script with directory name as argument in bash in that directory Bash Shell Scrpiting,如何编写脚本来运行要求输入的程序? - Bash Shell Scrpiting, How can I write a script to run a program which ask for input? 如何在带有参数的bash脚本中找到子文件夹? - How do I get to subfolders in a bash script that takes an argument? 我如何为Redhat Linux bash shell编写shell脚本 - How do I write shell script for Redhat Linux bash shell 是否可以编写一个在 bash/shell 和 PowerShell 中运行的脚本? - Is it possible to write one script that runs in bash/shell and PowerShell? 编写一个 linux 脚本,将目录作为参数,然后将该目录中最长的条目打印为 output - Write a linux script that takes a directory as an argument and then prints the longest entry in that directory as an output 如何通过bash获取一个bash脚本的目录名和文件名? - How to get the Directory name and file name of a bash script by bash? 使用shell或bash脚本基于目录名称删除多个目录的脚本 - Script for deleting multiple directories based on directory name using shell or bash script 如何编写 bash 脚本到 label 每个参数如下: - How to write a bash script to label each argument like this:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM