Schreiben eines Unix-Skripts (bash), das ein Verzeichnis an der in der Befehlszeile angegebenen Position erstellt

2457
David

Ich habe derzeit ein Unix-Skript (geschrieben in Bash), das mehrere Befehle ausführt, um eine Gruppe von Dateien zu ändern, die ich mit einer Liste mit ihren Pfaden angreife. Die allgemeine Idee des Skripts besteht darin, ein Verzeichnis zu erstellen, Dateien aus der Liste in diesem Verzeichnis abzulegen und anschließend die folgenden Prozesse auszuführen, von denen einige weitere Verzeichnisse erstellen.

Das Problem, das ich dabei habe, ist, dass ich den genauen Dateipfad angeben muss, wo die Verzeichnisse im Skript selbst liegen sollen. Meine Frage ist also: Gibt es eine Möglichkeit, das Skript so zu schreiben, dass ich den Befehl eingeben und dann den Pfad des Zielordners angeben kann, in dem ich das Skript in der Befehlszeile ausführen möchte, und die ursprünglichen und alle folgenden Verzeichnisse erstellen lassen durch das Skript innerhalb des Ausgangsverzeichnisses, ohne einen ihrer Pfade angeben zu müssen (z. B. 'Befehl / Ziel / Pfad / für / Skript /')?

Entschuldigung, wenn das etwas langatmig ist. Ich bin relativ neu in Unix und habe sehr wenig Erfahrung (und könnte daher keine besseren Formulierungen finden). Jede Hilfe wird geschätzt!

0

2 Antworten auf die Frage

3
terdon

Wenn ich Sie richtig verstehe, fragen Sie, wie Sie Werte an ein Bash-Skript übergeben. Dies ist zum Beispiel sehr einfach:

#!/usr/bin/env bash directory=$1; echo "Directory is $directory" 

$1ist das erste Befehlszeilenargument eines Bash-Skripts. $2ist das zweite usw. usw. Sie können das Skript also wie folgt ausführen:

./foo.sh /path/to/bar Directory is /path/to/bar 

Wenn Sie möchten, dass der Befehl auch eine Variable ist, können Sie Folgendes tun:

 #!/usr/bin/env bash command=$1; directory=$2 $command $directory 

Um auszuführen ls /etc, würden Sie das Skript wie folgt ausführen:

./foo.sh ls /etc 
+1 für die Präsentation einer tragbaren Lösung (unter Verwendung von env). Hennes vor 11 Jahren 0
Genau das habe ich gesucht. Vielen Dank!! David vor 11 Jahren 0
0
Ramillete

I imagine that you have files in a place (/path/to/originals) and want to copy them to a target location (/path/to/destination) and modify them afterwards. Your current script looks like:

mkdir /path/to/destination cp /originals/this-file /path/to/destination cp /originals/this-other-file /path/to/destination modify-somehow /path/to/destination/this-file modify-somehow /path/to/destination/this-other-file 

but you don't like to have to hardcode /path/to/destination everywhere. So you can ask to use "the value of the first positional parameter" instead of hardcoding /path/to/destination. As others mentioned, the value of the first positional parameter is $1.

So your script should be:

mkdir $1 cp /originals/this-file $1 cp /originals/this-other-file $1 modify-somehow $1/this-file modify-somehow $1/this-other-file 

And you should invoke this by adding the destination path as an argument:

my-script /path/to/destination 

I tried to keep the script simple, but you could improve it, like using a single cp command to copy several files. You can also use a variable for your /originals path (but instead of an argument, this one sounds like a constant declaration at the beginning of your script)

Lastly, consider that if your filenames have spaces, you'll need to surround your $1 in double quotes.