Jadi saya belakangan ini sedang membuat beberapa skrip bash yang mengambil parameter berupa path sebuah file. Perkaranya adalah bagaimana menangani string input untuk path yang absolute maupun relative. Ternyata cukup sederhana, yakni dengan readlink. Perintah ini akan mengambil absolute path dari input yang berupa relative path.
#!/bin/bash # Check whether a string represents a directory or not # string can be relative or absolute path dir=$(readlink -f $1) if [ -d $dir ]; then echo "$dir" else echo "$dir is not a directory" fi
Contohnya
petra@petra-laptop:~$ l ~/test mailer1.txt php54test.php test/ test3/ test3.php~ test4.php test5.php test6.php mailer2.txt php54test.php~ test2.php test3.php test4/ test4.php~ test5.php~ petra@petra-laptop:~$ ./test.sh ~/test /home/petra/test petra@petra-laptop:~$ ./test.sh /home/petra/test/test3 /home/petra/test/test3 petra@petra-laptop:~$ ./test.sh /home/petra/test/test3.php /home/petra/test/test3.php is not a directory petra@petra-laptop:~$ ./test.sh ../petra/test/test4.php /home/petra/test/test4.php is not a directory petra@petra-laptop:~$ ./test.sh ../petra/test/test3 /home/petra/test/test3