0

スクリプトを開いたファイルのディレクトリ(フルパスではない)を変数に入れるにはどうすればよいですか?これは$1と同じだと思いますので、ターミナルから実行したときにファイルのパスが同じ結果になるようにします。

説明;

#!/bin/bash
script.sh '~/foo/bar/file.ext'

#I want to put "bar" into a variable.

GUIを介して「file.ext」が「script.sh」で開く場合にも機能するはずです。

4

3 に答える 3

4

You should checkout both the dirname and basename programs, The dirname program takes an argument and removes the name of the program from the rest of the path. That is, it will give you the directory name.

The basename program does the opposite. Given the name of a path, it will remove all directories and just leave the file name:

$ dirname /one/two/three/four/five.txt
/one/two/three/four
$ basename /one/two/three/four/five.txt
five.txt

Now, the rest depends upon the shell. In BASH (which is the default shell on Linux and what you've tagged, you can use the $(command) syntax. This takes the output of the command and replaces it on the command line. For example:

$ mydirectory=$(dirname /one/two/three/four/five.txt)
$ echo $mydirectory
/one/two/three/four

In the above example, the dirname command took the name of the directory, replaced everything in the $() syntax and allowed me to set the name of my variable mydirectory.

You can use them in combination to get what you want:

$ my_full_dir=$(dirname "~/foo/bar/file.ext")
$ echo $my_full_dir
~/foo/bar
$ my sub_dir=$(basename $my_full_dir)
$ echo $sub_dir
bar

You can also combine the basename and dirname commands together:

$ my_sub_dir=$(basename $(dirname "~/foo/bar/file.ext"))
$ echo $my_sub_dir
$ bar

When a shell program executes, it puts each and every parameter on the command line into a count variable. For example:

$ myprog alpha beta gamma delta

Inside of the program myprog, the following variables are set:

$1 = "alpha"
$2 = "beta"
$3 = "gamma"
$4 = "delta"

One more thing with the BASH shell: There's a special filter syntax to parse variables.

  • ${variable#pattern} - Left small pattern. Removes the smallest pattern from the left side of the variable
  • ${variable##pattern} - Left large pattern. Removes the largest pattern from the left side of the variable
  • ${variable%pattern} - Right small pattern. Removes the smallest pattern from the right side of the variable
  • ${variable%%pattern} - Right large pattern. Removes the largest possible pattern from the right side of the variable

Here's an example:

$ FOO="ONE|TWO|THREE|FOUR"
$ echo ${FOO##*|}
FOUR
$ echo ${FOO#*|}
TWO|THREE|FOUR

In the above cases, the pattern was *|. This means any combination of letters followed by a | In the first one, the smallest match was ONE|. In the second one, it was ONE|TWO|THREE|. You can also use this to simulate the basename and dirname commands:

$ myfile="~/foo/bar/file.ext"
$ echo ${myfile%/*}     #Like `dirname`
~/foo/bar
$echo ${myfile#*/}      #Like `basename`
file.txt
于 2011-09-16T01:51:27.990 に答える
0

にコードを追加しますscript.sh

 echo '$1='"$1"

 inputFile=$1

 inputPath=${inputFile%/*}
 finalDir=${inputPath##*/}

 echo '$inputFile='"${inputFile}"
 echo '$inputPath='"${inputPath}"
 echo '$finalDir='"${finalDir}"

出力

  ...
  $finalDir=bar

これはあなたにあなたが望む結果を与えますか?

これがお役に立てば幸いです。

于 2011-09-16T01:15:40.230 に答える
0

あなたのシェルが/bin/bash

echo $(basename $(dirname $1))
于 2011-09-16T01:21:33.580 に答える