ksh - 653-401 Cannot rename using mv in a shell script -


what seem problem code

#/usr/bin/ksh rampath=/home/ram0 remotefile=site information_2013-07-11-00-01-56.csv  cd $rampath newfile=$(echo "$romotefile" | tr ' ' '_') mv  "$remotefile" "$newfile" 

error got after running script:

mv site information_2013-07-11-00-01-56.csv :653-401 cannot rename site information_2013-07-11-00-01-56.csv file or directory in path name not exist.

the file exist on directory. did putting double quotes in variable. same error above.

oldfile=$(echo "$remotefile" | sed 's/^/"/;s/$/"/' | sed 's/^m//') newfile=$(echo "$romotefile" | tr ' ' '_') mv  "$remotefile" "$newfile" 

there @ least 2 problems:

  1. the script has typo in variable name, @shelter suggested.
  2. the value assigned variable should quoted.

typo

newfile=$(echo "$romotefile" | tr ' ' '_') # returns empty string mv  "$remotefile" "$newfile" 

the shell permissive language. typos made.

one way of catching them force error on unset variables. -u option that. include set -u @ top of script, or run script ksh -u scriptname.

another way test individually each variable, adds overhead code.

newfile=$(echo "${romotefile:?}" | tr ' ' '_') mv  "${remotefile:?}" "${newfile:?}" 

the ${varname:?[message]} construct in ksh , bash generate error if variable varname unset or empty.

variable assignment

an assignment like

varname=word1 long-string 

must written as:

varname="word long-string" 

otherwise, read assignment varname=word in environment created command long-string.

$ remotefile=site information_2013-07-11-00-01-56.csv -ksh: information_2013-07-11-00-01-56.csv: not found [no such file or directory] $ remotefile="site information_2013-07-11-00-01-56.csv" 

as bonus, ksh allows replace characters during variable expansion ${varname//string1/string2} method:

$ newfile=${remotefile// /_} $ echo "$newfile" site_information_2013-07-11-00-01-56.csv 

if you're new (korn) shell programming, read manual page, sections on parameter expansion , variables.


Comments