LoginSignup
20
11

More than 5 years have passed since last update.

すぐ忘れるfish script syntax

Last updated at Posted at 2016-10-05

これは備忘録

更新予定:随時

script arguments

コマンドラインから与えられた引数の値は$argv配列(indexは1から始まる)を参照することで得られる。

argv, an array of arguments to the shell or function. argv is only defined when inside a function call, or if fish was invoked with a list of arguments, like fish myscript.fish foo bar. This variable can be changed by the user.

"Special variables" / http://fishshell.com/docs/current/index.html

hoge.fish
#!/usr/bin/env

echo $argv[1]
$ ./hoge.fish "fuga"
fuga

loop with command

コマンド実行結果を元にループ処理する場合

loop.fish
#!/usr/bin/env fish

for i in (ls *.mp4)
    mv $i (string replace .mp4 .m4a $i)
end
$ ls *.mp4
hoge.mp4
fuga.mp4
bar.mp4

$ ./loop.fish

$ ls *.m4a
hoge.m4a
fuga.m4a
bar.m4a

上記の処理に限っては、Finderだけで一括変換できるが、わかりやすい例として挙げておく。

Operators for files and directories

下記の-eんとこのスイッチリスト

if test -e /var/path/hoge.log
 echo 'available'
end
-b FILE returns true if FILE is a block device.
-c FILE returns true if FILE is a character device.
-d FILE returns true if FILE is a directory.
-e FILE returns true if FILE exists.
-f FILE returns true if FILE is a regular file.
-g FILE returns true if FILE has the set-group-ID bit set.
-G FILE returns true if FILE exists and has the same group ID as the current user.
-L FILE returns true if FILE is a symbolic link.
-O FILE returns true if FILE exists and is owned by the current user.
-p FILE returns true if FILE is a named pipe.
-r FILE returns true if FILE is marked as readable.
-s FILE returns true if the size of FILE is greater than zero.
-S FILE returns true if FILE is a socket.
-t FD returns true if the file descriptor FD is a terminal (TTY).
-u FILE returns true if FILE has the set-user-ID bit set.
-w FILE returns true if FILE is marked as writable; note that this does not check if the filesystem is read-only.
-x FILE returns true if FILE is marked as executable.
20
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
11