![]() |
![]() ![]() |
![]() |
![]()
Post
#1
|
|
![]() Grupa: Zarejestrowani Postów: 91 Pomógł: 0 Dołączył: 19.02.2004 Skąd: Piaseczno Ostrzeżenie: (0%) ![]() ![]() |
Potrzebuje pomocy.
Jak z czegos takiego: /home/roman/nazwa_pliku dostac samą nazwe pliku w shellu? Może zna ktoś jakies dobre forum o pisaniu skrytów w shellu? pozdrawiam |
|
|
![]()
Post
#2
|
|
![]() Developer Grupa: Moderatorzy Postów: 2 844 Pomógł: 20 Dołączył: 25.11.2003 Skąd: Olkusz ![]() |
moge dac glowe ze Serafin wie jak
![]() |
|
|
![]()
Post
#3
|
|
![]() Grupa: Przyjaciele php.pl Postów: 5 724 Pomógł: 259 Dołączył: 13.04.2004 Skąd: N/A Ostrzeżenie: (0%) ![]() ![]() |
basename
jak w php -------------------- Nie lubię jednorożców.
|
|
|
![]()
Post
#4
|
|
![]() Grupa: Zarejestrowani Postów: 91 Pomógł: 0 Dołączył: 19.02.2004 Skąd: Piaseczno Ostrzeżenie: (0%) ![]() ![]() |
dzięki!
mam jeszcze pytania:) Czy da się jakos wyciagnac info o katalgu, w którym znajduje sie skrypt. Załozmy, że w skrypcie jest komenda "cd podkatalog-katalgu-w-ktorym-jest-skrypt" Chodzi mi o to, że jesli użytkownik znajduje sie powiedzmy w "/" i odpali skrypt tak: /home/roman/skrypt to nie zadziala, bo pwd bedzie ustawione na "/" wiec cd zwroci, ze katalog, do którego probuje wejsc nie istnieje. Co ztym zrobic? thx za pomoc |
|
|
![]()
Post
#5
|
|
![]() Grupa: Przyjaciele php.pl Postów: 5 724 Pomógł: 259 Dołączył: 13.04.2004 Skąd: N/A Ostrzeżenie: (0%) ![]() ![]() |
Kod #DIRECTORY bedzie mialo wartosc sciezki do skryptu
DIRECTORY=`dirname $0` #tu sa backticki cd "$DIRECTORY/katalog/" cat jakisplik_w_tym_katalogu.txt -------------------- Nie lubię jednorożców.
|
|
|
![]()
Post
#6
|
|
![]() Grupa: Zarejestrowani Postów: 91 Pomógł: 0 Dołączył: 19.02.2004 Skąd: Piaseczno Ostrzeżenie: (0%) ![]() ![]() |
ok dzięki za pomoc:)
Jak wyciagnac z /dir2/dir1/plik tylko /dir2/dir1/ czyli samą sciezke? Dzięki za pomoc! |
|
|
![]()
Post
#7
|
|
![]() Grupa: Przyjaciele php.pl Postów: 1 717 Pomógł: 0 Dołączył: 12.06.2002 Skąd: Wolsztyn..... Studia: Zielona Góra Ostrzeżenie: (0%) ![]() ![]() |
dirname
Do tego mogles dojsc wpisujac man basename i ogladajac funkcje pokrewne... -------------------- Brak czasu :/
|
|
|
![]()
Post
#8
|
|
![]() Grupa: Zarejestrowani Postów: 866 Pomógł: 32 Dołączył: 2.06.2004 Skąd: Wrocław Ostrzeżenie: (0%) ![]() ![]() |
Szybki kursik Bash'a?
![]() -------------------- |
|
|
![]()
Post
#9
|
|
![]() Grupa: Zarejestrowani Postów: 91 Pomógł: 0 Dołączył: 19.02.2004 Skąd: Piaseczno Ostrzeżenie: (0%) ![]() ![]() |
szukałem dirname przez 'apropos' i nie znalazlem.. a szkoda;)
zroiblęm tak: Kod local old_name=`basename "$1"` local old_path=${1/%"$old_name"/} Zdecydowanie szybki:) Ale idzie całkiem dobrze udalo mi sie napisac cos takiego: Kod #!/bin/bash # Error codes # 1 - no pattern chosen # 2 - no files given # 3 - user has no rights for filename modification or file does not exist # 4 - filename with name that is to be assigned to another file already exists # 5 - mv failed # 6 - wrong sed expression format function Modify() { #echo "I'm in modify $@" for file in "$@" do # if we talk about .. or . just skip it if [ "$1" = ".." ] || [ "$1" = "." ]; then continue fi #if user has no rights to modify file exit if [ ! -w "$1" ]; then echo "$1 File does not exist or You do not have needed permissions." exit 3 fi #if this is a directory and recursion was set penetrate it if [ -d "$1" ] && [ "$recursion" = "true" ]; then pushd "$1" > /dev/null 2>&1 #echo "entering $1" Modify `ls -A` #echo "leaving $1" popd > /dev/null 2>&1 fi #obtain path and filename local old_name=`basename "$1"` local old_path=${1/%"$old_name"/} #echo "Old path: $old_path" #modify filename case "$mod_type" in upper) new_name=`echo "$old_name" | tr '[:lower:]' '[:upper:]'` ;; lower) new_name=`echo "$old_name" | tr '[:upper:]' '[:lower:]'` ;; sed) new_name=`echo "$old_name" | sed "$sed_pattern"` if [ new_name = "" ]; then echo "Wrong sed expression format." exit 6 fi ;; esac #check if we will not overwrite smthing if [ -e "$old_path$new_name" -a ! "$old_path$new_name" = "$old_path$old_name" ]; then echo "File with the name $new_name already exists, cannot modify." exit 4 fi if [ "$new_name" = "$old_name" ]; then continue fi if ! mv "$old_path$old_name" "$old_path$new_name"; then pwd echo "Mv failed to change filename." exit 5 fi changed=$((changed + 1)) echo "Filename changed from $old_name to $new_name;" shift done } ShowHelp () { cat << EOF Modify script modifies filnames according to given arguments. Syntax: modify [-r] [-u/-l/sed_pattern] FILES.. Modify takes only one parameter, following values are allowed: -u make all filenames uppercase -l make all filenames lowercase sed pattern - maodifies filnames accordingly to given pattern Options: -r modfications are made recursivly Author: Roman Baluta <balutar@o2.pl> EOF } ShowUsage() { echo -e "\nPlease use: modify --help to get information on using this script.\n" } #init variables mod_chosen="false" mod_type="" sed_pattern="" recursion="false" changed=0 #echo "#of arguments: $#" #if -h or --help printout help if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then ShowHelp exit 0 fi #this loop retrieves arguments for arg in "$@" do case "$1" in -u) #if modify method already chosen then this is not an rgument but file name if [ "$mod_chosen" = "true" ]; then break fi mod_type="upper" mod_chosen="true" shift ;; -l) #if modify method already chosen then this is not an rgument but file nam if [ "$mod_chosen" = "true" ]; then break fi mod_type="lower" mod_chosen="true" shift ;; -r) #if modify method already chosen then this is not an rgument but file name if [ "$mod_chosen" = "true" ] || [ "$recursion" = "true" ] ; then break fi recursion="true" shift ;; #determine wheather it is not a try of passing sed pattaren *) #if method is already chosen then this can only be a filename if [ "$mod_chosen" = "true" ]; then break fi mod_type="sed" mod_chosen="true" sed_pattern="$1" shift ;; esac done #echo "#of files: $#" #echo mod_type:$mod_type #echo mod_chosen:$mod_chosen #if no method was chosen exit and display usage info if [ "$mod_chosen" = "false" ]; then ShowUsage exit 1 fi if [ "$#" = "0" ]; then ShowUsage exit 2 fi #rename files given as next arguments with the selected pattern Modify "$@" echo -e "\n$changed filename(s) changed.\n" exit 0 Modyfikuje nazwy plików (-l to lowercase, -u to upper case, albo korzysta seda) no i -r powoduje, ze dziala rekursywnie. Po co pisac cos takiego? -> uczelnia:) Jesli przypadkiem zobaczycie jakies błędy albo cos co mozna zrobic lepiej to dajcie znac, dzieki za pomoc! Ten post edytował rmn 22.04.2005, 16:24:49 |
|
|
![]() ![]() |
![]() |
Aktualny czas: 20.08.2025 - 11:51 |