如何创建使用变量执行大量命令的命令行(unix / linux)指令?

时间:2022-12-03 20:45:37

I need to rearrange some content in various directories but it's a bit of a pain. In order to debug the application I'm working on (a ruby app) I need to move my gems into my gem folder one at a time (long story; nutshell: one is broken and I can't figure out which one).

我需要重新安排各种目录中的一些内容,但这有点痛苦。为了调试我正在处理的应用程序(一个ruby应用程序),我需要一次一个地将我的宝石移动到我的gem文件夹中(长篇故事;简言之:一个被打破,我无法弄清楚哪一个)。

So I need to do something like:

所以我需要做一些事情:

sudo mv backup/gem/foo gem/
sudo mv backup/doc/foo doc/
sudo mv backup/specification/foo.gemspec specification/

replacing "foo" each time. How can I author a simple shell script to let me do something like: gemMove("foo") and it fill in the blanks for me?

每次都替换“foo”。我怎样才能创建一个简单的shell脚本让我做一些像gemMove(“foo”)这样的东西,它为我填充空白?

3 个解决方案

#1


4  

put the following into a file named gemmove:

将以下内容放入名为gemmove的文件中:

#!/bin/bash

foo=$1

if [ x$foo == x ]; then
  echo "Must have an arg"
  exit 1
fi

for d in gem doc specification ; do 
  mv backup/$d/$1 $d
done

then do

chmod a+x gemmove

and then call 'sudo gemmove foo' to move the foo gem from the backup dirs into the real ones

然后调用'sudo gemmove foo'将foo gem从备份dirs移动到真实的

#2


1  

You could simply use the bash shell arguments, like this:

你可以简单地使用bash shell参数,如下所示:

#!/bin/bash
# This is move.sh
mv backup/gem/$1 gem/
mv backup/doc/$1 doc/
# ...

and then execute it as:

然后执行它:

sudo ./move.sh foo

Be sure make the script executable, with

确保使脚本可执行

chmod +x move.sh

#3


1  

in bash, something like:

在bash中,类似于:

function gemMove()
{
filename=$1
   mv backup/gem/$filename gem/$filename
   mv backup/doc/$filename doc/$filename
   mv backup/specification/$filename.spec specification
}

then you can just call gemMove("foo") elsewhere in the script.

那么你可以在脚本的其他地方调用gemMove(“foo”)。

#1


4  

put the following into a file named gemmove:

将以下内容放入名为gemmove的文件中:

#!/bin/bash

foo=$1

if [ x$foo == x ]; then
  echo "Must have an arg"
  exit 1
fi

for d in gem doc specification ; do 
  mv backup/$d/$1 $d
done

then do

chmod a+x gemmove

and then call 'sudo gemmove foo' to move the foo gem from the backup dirs into the real ones

然后调用'sudo gemmove foo'将foo gem从备份dirs移动到真实的

#2


1  

You could simply use the bash shell arguments, like this:

你可以简单地使用bash shell参数,如下所示:

#!/bin/bash
# This is move.sh
mv backup/gem/$1 gem/
mv backup/doc/$1 doc/
# ...

and then execute it as:

然后执行它:

sudo ./move.sh foo

Be sure make the script executable, with

确保使脚本可执行

chmod +x move.sh

#3


1  

in bash, something like:

在bash中,类似于:

function gemMove()
{
filename=$1
   mv backup/gem/$filename gem/$filename
   mv backup/doc/$filename doc/$filename
   mv backup/specification/$filename.spec specification
}

then you can just call gemMove("foo") elsewhere in the script.

那么你可以在脚本的其他地方调用gemMove(“foo”)。