One of the most common commands on the terminal is -f file && command
; it checks if the file exists and runs the command if it does. Do you know it's the test
? A condition evaluation utility according to its manual page.
Whoever wrote a shell script had used it a lot with its brackets to evaluate expressions, a useful utility, a complete tool belt to check files - on UNIX, almost everything is represented as a file, block device, terminals, sockets - directories, and much more.
Lately, playing with Crystal to configure my local machine, I was converting a few bash scripts and thought: is there anything like test
to help me with this conversion? Sadly, I couldn't find it.
If you are reading this, you had better luck than me. Below is an extensive list (in the same order as the manual), with all test options and respective Crystal commands as one-liners. Okay, a few are two lines because I wanted to clarify in the variable name what that value is, but you can use it as a one-liner.
I hope you enjoy it 😄
True if file exists and is a block special file.
File.info("/dev/disk0").type.block_device?
True if file exists and is a character special file.
File.info("/dev/console").type.character_device?
True if file exists and is a directory.
File.info("/dev").type.directory?
True if file exists (regardless of type).
File.exists?("/dev")
True if file exists and is a regular file.
File.file?("/etc/passwd")
True if file exists and its set group ID flag is set.
File.info("./sgid_file").flags.includes?(File::Flags::SetGroup)
True if file exists and is a symbolic link. This operator is retained for compatibility with previous versions of this program. Do not rely on its existence; use -L instead.
File.symlink?("linkz")
True if file exists and its sticky bit is set.
File.info("sticky_bit_file").flags.includes?(File::Flags::Sticky)
True if the length of string is nonzero.
"a".size > 0
True if file is a named pipe (FIFO).
File.info("fifo_file").type.pipe?
True if file exists and is readable.
File.readable?("readable")
True if file exists and has a size greater than zero.
File.size("foo.sh") > 0
True if the file whose file descriptor number is file_descriptor is open and is associated with a terminal. It doesn't apply
True if file exists and its set user ID flag is set.
File.info("./suid_file").flags.includes?(File::Flags::SetUser)
True if file exists and is writable. True indicates only that the write flag is on. The file is not writable on a read-only file system even if this test indicates true.
File.writable?("./writable")
True if file exists and is executable. True indicates only that the execute flag is on. If file is a directory, true indicates that file can be searched.
File.executable?("./foo.sh")
True if the length of string is zero.
"".size == 0
True if file exists and is a symbolic link.
File.symlink?("linkz")
True if file exists and its owner matches the effective user id of this process.
current_user_id = `id -u`.chomp
File.info("foo.sh").owner_id == current_user_id
True if file exists and its group matches the effective group id of this process.
current_group_id = id -g
.chomp
File.info("foo.sh").group_id == current_group_id
True if file exists and is a socket.
File.info(socket_file).type.socket?
True if file1 exists and is newer than file2.
File.info(newer_file).modification_time > File.info(older_file).modification_time
True if file1 exists and is older than file2.
File.info(older_file).modification_time < File.info(newer_file).modification_time
True if file1 and file2 exist and refer to the same file. Given on manual it specifies:
If file is a symbolic link, test will fully dereference it and then evaluate the expression against the file referenced, except for the -h and -L primaries.
File.same?("foo.sh", "linkz", follow_symlinks: true) # So we need to follow symlinks
True if string is not the null string.
str = ""
!str.nil? # true
str = nil
!str.nil? # false
True if the strings s1 and s2 are identical.
str1 == str2
True if the strings s1 and s2 are not identical.
str1 != str2
True if string s1 comes before s2 based on the binary value of their characters.
str1 < str2
True if string s1 comes after s2 based on the binary value of their characters.
str1 > str2
True if the integers n1 and n2 are algebraically equal.
1 == 2
True if the integers n1 and n2 are not algebraically equal.
1 != 2
True if the integer n1 is algebraically greater than the integer n2.
1 > 2
True if the integer n1 is algebraically greater than or equal to the integer n2.
2 > 2
True if the integer n1 is algebraically less than the integer n2.
1 < 2
True if the integer n1 is algebraically less than or equal to the integer n2.
2 < 2
Are you still reading it? Yes, sadly, this is the end. At least now you are ready to have some fun with shell script and crystal 😁👍