How to detect the current directory in which I run my shell script?

我如何检测运行shell脚本的目录名称(或更好的整个路径)? what shell? What operating system? For starters try man pwd $PWD 我相信这是最便携的方式: dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P) This is not as trivial as it looks like. Check out this question and this

如何检测我运行我的shell脚本的当前目录?

我如何检测运行shell脚本的目录名称(或更好的整个路径)? 什么外壳? 什么操作系统? 对于初学者尝试 man pwd $PWD 我相信这是最便携的方式: dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P) 这并不像看起来那么微不足道。 看看这个问题和这个

Using curl to upload POST data with files

I would like to use cURL to not only send data parameters in HTTP POST but to also upload files with specific form name. How should I go about doing that ? HTTP Post parameters: userid = 12345 filecomment = This is an image file HTTP File upload: File location = /home/user1/Desktop/test.jpg Form name for file = image (correspond to the $_FILES['image'] at the PHP side) I figured

使用curl将文件上传POST数据

我想使用cURL不仅在HTTP POST中发送数据参数,而且还要上传具有特定表单名称的文件。 我应该怎么做呢? HTTP发布参数: userid = 12345 filecomment =这是一个图像文件 HTTP文件上传:文件位置= /home/user1/Desktop/test.jpg file = image的表单名称(对应PHP侧的$ _FILES ['image']) 我认为cURL命令的一部分如下: curl -d "userid=1&filecomment=This is an image file" --data-binary @"/home/user1/D

name` pattern that matches multiple patterns

I was trying to get a list of all python and html files in a directory with the command find Documents -name "*.{py,html}" . Then along came the man page: Braces within the pattern ('{}') are not considered to be special (that is, find . -name 'foo{1,2}' matches a file named foo{1,2}, not the files foo1 and foo2. As this is part of a pipe-chain, I'd like to be

名称“模式匹配多个模式

我试图通过命令find Documents -name "*.{py,html}"获取目录中所有python和html文件的列表。 接下来是手册页: 模式中的大括号('{}')不被认为是特殊的(即查找。-name'foo {1,2}'匹配名为foo {1,2}的文件,而不是文件foo1和foo2。 由于这是管道链的一部分,我希望能够指定它在运行时匹配哪些扩展(无硬编码)。 如果找不到,perl单行(或类似)就可以。 编辑:我最终提出的答案包括各种废

Shell command to sum integers, one per line?

I am looking for a command that will accept as input multiple lines of text, each line containing a single integer, and output the sum of these integers. As a bit of background, I have a log file which includes timing measurements, so through grepping for the relevant lines, and a bit of sed reformatting I can list all of the timings in that file. I'd like to work out the total however, an

Shell命令来整数整数,每行一个?

我正在寻找一个命令,它将接受多行文本作为输入,每行包含一个整数,并输出这些整数的总和。 作为一个背景,我有一个日志文件,其中包括时间测量,所以通过对相关行进行grep,以及一些sed格式化,我可以列出该文件中的所有时间。 然而,我想要计算出总数,而我的想法却一片空白,我可以用这个中间输出来执行最后的总和。 过去我一直使用expr ,但除非它运行在RPN mode ,否则我不认为它会应付这种情况(即使这样也会很棘手)

How do you tell if a string contains another string in Unix shell scripting?

I want to write a Unix shell script that will do various logic if there is a string inside of another string. For example, if I am in a certain folder, branch off. Could someone please tell me how to accomplish this? If possible I would like to make this not shell specific (ie not bash only) but if there's no other way I can make do with that. #!/usr/bin/env sh if [ "$PWD" contains "Stri

你如何判断一个字符串在Unix shell脚本中是否包含另一个字符串?

我想编写一个Unix shell脚本,如果在另一个字符串中有一个字符串,它将执行各种逻辑。 例如,如果我在某个文件夹中,则关闭。 有人可以告诉我如何做到这一点? 如果可能的话,我想让这不是特定的shell(即不是bash),但是如果没有别的办法,我可以用它做。 #!/usr/bin/env sh if [ "$PWD" contains "String1" ] then echo "String1 present" elif [ "$PWD" contains "String2" ] then echo "String2 present" else

How to scp a folder from remote to local?

I am not sure whether it is possible to scp a folder from remote to local, but still I am left with no other options. I use ssh to log into my server and from there I would like to copy the folder foo to home/user/Desktop (my local). Is there any command so that I can do this? scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/ 从man scp -r Recursively copy entire directories

如何scp文件夹从远程到本地?

我不知道是否有可能scp从远程文件夹到本地,但还是我留下没有其他选择。 我使用SSH登录到我的服务器,并从那里我想将文件夹foo复制到home/user/Desktop (我的本地)。 有没有任何命令可以做到这一点? scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/ 从man scp -r Recursively copy entire directories 要使用scp的全部功能,您需要完成以下步骤: 公钥授权 创建ssh别名 然后,例如,如果你

'echo' without newline in a shell script

I have a problem with echo in my script: echo -n "Some string..." prints -n Some string... and moves to the next line. In the console it's working correcly without newline: Some string... There are multiple versions of the echo command, with different behaviors. Apparently the shell used for your script uses a version that doesn't recognize -n . The printf command has much more

'echo'在shell脚本中没有换行符

我在脚本中遇到了echo问题: echo -n "Some string..." 版画 -n Some string... 并移动到下一行。 在控制台中它正常工作,没有换行符: Some string... 有多个版本的echo命令,具有不同的行为。 显然,用于脚本的外壳使用不能识别-n的版本。 printf命令具有更一致的行为。 echo是罚款喜欢简单的事情echo hello ,但我建议使用printf任何事情更加复杂。 你在哪个系统上,你的脚本使用了什么shell? bash有一个名为

In a shell script: echo shell commands as they are executed

In a shell script how do I echo all shell commands called and expand any variable names? For example, given the following line: ls $DIRNAME I would like the script to run the command and display the following ls /full/path/to/some/dir The purpose is to save a log of all shell commands called and their arguments. Perhaps there is a better way of generating such aa log? set -x or set -o xtr

在shell脚本中:echo shell命令执行时

在shell脚本中,我如何回显所有调用的shell命令并展开所有变量名称? 例如,给出以下行: ls $DIRNAME 我希望脚本运行该命令并显示以下内容 ls /full/path/to/some/dir 目的是保存所有调用的shell命令及其参数的日志。 也许有更好的方法来生成这样的日志? set -x或set -o xtrace扩展变量并在行之前打印一点+符号。 set -v或set -o verbose在打印之前不会展开变量。 使用set +x和set +v关闭上述设置。 在脚本的第

How to mkdir only if a dir does not already exist?

I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the mkdir command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that the directory does not exist, or suppress the "File exists" error that mkdir throws when it tries to create an existing directory. Any t

仅当dir不存在时如何mkdir?

我正在编写一个shell脚本,以便在AIX上的KornShell(ksh)下运行。 我想使用mkdir命令来创建一个目录。 但目录可能已经存在,在这种情况下,我不想做任何事情。 因此,我想要测试以查看目录不存在,或者抑制mkdir在尝试创建现有目录时抛出的“文件存在”错误。 任何想法如何最好地做到这一点? 尝试mkdir -p : mkdir -p foo 请注意,这也会创建任何不存在的中间目录; 例如, mkdir -p foo/bar/baz 将创建目录foo , f

How to use SSH to run a shell script on a remote machine?

I have to run a shell script (windows/Linux) on a remote machine. I have SSH configured on both machine A and B. My script is on machine A which will run some of my code on a remote machine, machine B. The local and remote computers can be either Windows or Unix based system. Is there a way to run do this using plink/ssh? If Machine A is a Windows box, you can use Plink (part of PuTTY) wi

如何使用SSH在远程机器上运行shell脚本?

我必须在远程机器上运行一个shell脚本(windows / Linux)。 我在机器A和B上都配置了SSH。我的脚本在机器A上,它将在远程机器B上运行我的一些代码。 本地和远程计算机可以是基于Windows或Unix的系统。 有没有办法使用plink / ssh来执行此操作? 如果机器A是Windows机器,则可以使用Plink(PuTTY的一部分)和-m参数,它将在远程服务器上执行本地脚本。 plink root@MachineB -m local_script.sh 如果机器A是基于Unix的系