shell script to remove a file if it already exist

I am working on some stuff where I am storing data in a file. But each time I run the script it gets appended to the previous file. I want help on how I can remove the file if it already exists. 我使用的另一条命令是: [ -e file ] && rm file Don't bother checking if the file exists, just try to remove it. rm -f /p/a/t/h # or rm /p/a/t/h 2> /dev/null But more likely, if you ar

shell脚本删除文件,如果它已经存在

我正在研究一些将数据存储在文件中的东西。 但是每次运行脚本时,它都会附加到前一个文件。 如果文件已经存在,我需要帮助我删除文件。 我使用的另一条命令是: [ -e file ] && rm file 不要打扰检查文件是否存在,只是尝试删除它。 rm -f /p/a/t/h # or rm /p/a/t/h 2> /dev/null 但更有可能的是,如果您追加到文件,这是因为您的脚本正在使用>>将某些内容重定向到文件中。 只需更换>>与> 。

Docker Copying file from host to container

I am trying to copy a set of files from docker host to container. On a AUFS system directly going into /var/lib/docker/aufs/... works. However I am another system with Fedora that has devicemapper as the storage driver. On this system if I do this: [root@myhost tmp]# docker inspect -f '{{.Id}}' 393ef4b9f485 393ef4b9f485dafc78037f59bdbeda16d63b8338487248ed25b68cf544f29e24 [root@myhost tmp]# cd

Docker将文件从主机复制到容器

我正试图将一组文件从docker主机复制到容器中。 在直接进入/ var / lib / docker / aufs / ...的AUFS系统上工作。 不过,我是Fedora的另一个系统,它使用devicemapper作为存储驱动程序。 在这个系统上,如果我这样做: [root@myhost tmp]# docker inspect -f '{{.Id}}' 393ef4b9f485 393ef4b9f485dafc78037f59bdbeda16d63b8338487248ed25b68cf544f29e24 [root@myhost tmp]# cd /var/lib/docker/devicemapper/mnt/393ef4b9f485

read file name from text file in ksh and compress

I want to write a KornShell (ksh) script to read filename from a textfile(having list of prefixes) and compress it if it is found in the directory by looping. ex: I will keep the prefix 'abcd' in the text file . By reading it, I want to compress the files in the directory matching the name like this abcd###.YYYYMMDDXXXXXX.txt I do not want to do anything with same prefix but with di

从文本文件中以ksh和compress读取文件名

我想编写一个KornShell(ksh)脚本来从文本文件(有前缀列表)中读取文件名,并通过循环在目录中找到它进行压缩。 例如:我会在文本文件中保留前缀'abcd'。 通过阅读它,我想压缩匹配名称的目录中的文件,例如abcd ###。YYYYMMDDXXXXXX.txt 我不想使用相同的前缀做任何事情,但使用不同的扩展名或不同的模式,例如abcd ###。YYYYMMDDXXXXXX.dat或abcd ###。YYYYMMDDXXXXXX.txt.Z。 我只想压缩像这样abcd ###。YYY

Strange behaviour of

My KornShell (ksh) manual says that -d expression returns true if the file exists and it is a directory. So if [[ -d file ]] should return TRUE if file is a directory. But in my case this is not how it works. It returns TRUE if the file exists and is NOT a directory, but the manual of the shells says "and it is a directory". So does anyone have any idea why it is working in the oppo

奇怪的行为

我的KornShell(ksh)手册说,如果文件存在并且它是一个目录, -d表达式返回true。 因此if [[ -d file ]]如果file是一个目录, if [[ -d file ]]应该返回TRUE。 但在我的情况下,这不是它的工作原理。 如果文件存在并且不是目录,它会返回TRUE,但是shell的手册说“它是一个目录”。 那么,有没有人知道它为什么与它应该是相反的? 它工作正常; 这是你的期望是错误的。 在shell中, 0返回值为true ,并且非零返回值为false

Running JAR file on AIX via KornShell script

I run the following command on my AIX machine. /usr/java6_64/jre/bin/java -jar myapp.jar Then, things look fine. The JAR file connects to the database and does whatever it needs to do. But I need to put this command (plus a few others) in a script. So I created a KornShell (ksh) script file called "script.ksh" to do the above. #!/bin/ksh /usr/java6_64/jre/bin/java -jar myapp.jar

通过KornShell脚本在AIX上运行JAR文件

我在AIX机器上运行以下命令。 /usr/java6_64/jre/bin/java -jar myapp.jar 然后,事情看起来很好。 JAR文件连接到数据库并执行所需的任何操作。 但我需要把这个命令(加上其他几个)放在一个脚本中。 所以我创建了一个名为“script.ksh”的KornShell(ksh)脚本文件来完成上述操作。 #!/bin/ksh /usr/java6_64/jre/bin/java -jar myapp.jar 但它给了我以下例外: EXCEPTION: TerminateProcessException: Cannot connect

KornShell Boolean Conditional Logic

I am a little confused with this KornShell (ksh) script I am writing, mostly with booleans and conditionals. So the first part of my script I have catme and wcme both set to either true or false . This part is working fine, as I have tried echo ing them and they produce the expected results. Later on, I have this code: if [[ $catme ]] ; then some commands fi And I repeat this with wcme

KornShell布尔条件逻辑

我对这个我写的KornShell(ksh)脚本有点困惑,主要是布尔和条件。 所以我的脚本的第一部分我有catme和wcme都设置为true或false 。 这部分工作正常,因为我试图echo他们,他们产生了预期的结果。 后来,我有这样的代码: if [[ $catme ]] ; then some commands fi 我用wcme重复wcme 。 然而,意外的是,无论wcme和catme是什么,我的if语句中的命令都会被执行。 这是一个语法错误? 我试过[[ $catme -eq true ]]但

How do I put an already

I have a process that is already running for a long time and don't want to end it. How do I put it under nohup (that is, how do I cause it to continue running even if I close the terminal?) Using the Job Control of bash to send the process into the background: Ctrl+Z to stop (pause) the program and get back to the shell. bg to run it in the background. disown -h [job-spec] where [jo

我如何放置已经

我有一个已经运行了很长时间的进程,不想结束它。 我该如何将它置于nohup之下(即,即使关闭终端,我如何使它继续运行?) 使用bash的作业控制将进程发送到后台: 按Ctrl + Z停止(暂停)程序并返回到shell。 bg在后台运行它。 disown -h [job-spec]其中[job-spec]是作业编号(如第一个运行作业的%1 ;使用jobs命令查找您的编号),以便在终端关闭时不会终止作业。 假设由于某种原因,Ctrl + Z也不起作用,转到另一个

I just started learning Scripting in Shell Script, RHEL 6.3

This question already has an answer here: Check if a directory exists in a shell script 32 answers The simplest technique is to use mkdir -p which creates the directory — and any missing directories up the path — and does not fail if the directory already exists, so you can do it all at once with: mkdir -p "$LOCAL_WORKING_DIR" "$LOCAL_EVENT_LOG_WORKING_DIR" Note that the variables are enclo

我刚开始学习RHEL 6.3 Shell脚本中的脚本

这个问题在这里已经有了答案: 检查一个目录是否存在于shell脚本中32个答案 最简单的方法是使用mkdir -p创建目录 - 以及路径中缺少的任何目录 - 如果该目录已经存在,则不会失败,因此您可以一次完成以下操作: mkdir -p "$LOCAL_WORKING_DIR" "$LOCAL_EVENT_LOG_WORKING_DIR" 请注意,这些变量用双引号引起来,即使名称中出现空格(等),它也会运行正常。

Delete lines in a text file that contain a specific string

我将如何使用sed删除包含特定字符串的文本文件中的所有行? To remove the line and print the output to standard out: sed '/pattern to match/d' ./infile To directly modify the file: sed -i '/pattern to match/d' ./infile To directly modify the file (and create a backup): sed -i.bak '/pattern to match/d' ./infile For Mac OS X users: sed -i '' '/pattern/d' ./infile there are many other ways to de

删除包含特定字符串的文本文件中的行

我将如何使用sed删除包含特定字符串的文本文件中的所有行? 要删除该行并将输出打印到标准输出: sed '/pattern to match/d' ./infile 要直接修改文件: sed -i '/pattern to match/d' ./infile 直接修改文件(并创建备份): sed -i.bak '/pattern to match/d' ./infile 对于Mac OS X用户: sed -i '' '/pattern/d' ./infile 除sed外,还有许多其他方法可以删除特定字符串 AWK awk '!/pattern/' file > temp &

Using wget to recursively fetch a directory with arbitrary files in it

I have a web directory where I store some config files. I'd like to use wget to pull those files down and maintain their current structure. For instance, the remote directory looks like: http://mysite.com/configs/.vim/ .vim holds multiple files and directories. I want to replicate that on the client using wget. Can't seem to find the right combo of wget flags to get this done. Any

使用wget以递归方式获取包含任意文件的目录

我有一个网络目录,我存储一些配置文件。 我想使用wget来拉下这些文件并保持其当前结构。 例如,远程目录如下所示: http://mysite.com/configs/.vim/ .vim保存多个文件和目录。 我想用wget在客户端复制它。 似乎无法找到正确的组合wget标志来完成这件事。 有任何想法吗? 您必须将-np / --no-parent选项传递给wget (当然除了-r / --recursive之外),否则它将跟随我站点上的目录索引中的链接到父目录。 所以这个命令