Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Shell scripting
Forum PHP.pl > Inne > Hydepark
rmn
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
hwao
moge dac glowe ze Serafin wie jak smile.gif ale ma mature jak go dorwe to skieruje tu:)
dr_bonzo
basename
jak w php
rmn
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
dr_bonzo
Kod
#DIRECTORY bedzie mialo wartosc sciezki do skryptu
DIRECTORY=`dirname $0` #tu sa backticki
cd "$DIRECTORY/katalog/"
cat jakisplik_w_tym_katalogu.txt
rmn
ok dzięki za pomoc:)

Jak wyciagnac z /dir2/dir1/plik tylko /dir2/dir1/ czyli samą sciezke?

Dzięki za pomoc!
FiDO
dirname

Do tego mogles dojsc wpisujac man basename i ogladajac funkcje pokrewne...
sztosz
Szybki kursik Bash'a? winksmiley.jpg
rmn
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!
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.