Need help in resolving a small error in unix shell scripting - shell

I am having a problem with a unix shell script.
The script is for renaming the Files and directories which is having spaces with "-" recursively.
Example "Stack Overflow" to "Stack-Overflow"
This is running perfectly and working , But I get this wierd error which I don't know where it is coming.
I am getting the below error though (last line),
./script.sh[164]: [: Test.dat: unexpected operator/operand
I know it is coming some where in below funtions,
fix_dirs
fix_files
my_rename
but not sure where.
Could some one please help me in finding the error (Sorry for the long script :) )?.
#!/usr/bin/ksh
USAGE="$0 -f directory
$0 -d directory
$0 -d -f directory
-f rename files
-d rename directories
"
usage ()
{
print -u2 "$USAGE"
exit 1
}
pathname ()
{
print -- "${1%/*}"
}
basename ()
{
print -- "${1##*/}"
}
find_dirs ()
{
find "$1" -depth -type d -name '* *' -print
}
find_files ()
{
find "$1" -depth -type f -name '* *' -print
}
my_rename()
{
FROM_DIR="$1";
TO_DIR="$2";
if [ ! -w ${FROM_DIR##*/} ]
then
echo "Directory/file ${FROM_DIR##*/} Not Writable";
exit 1
fi
if [ -d "$TO_DIR" -o -f "TO_DIR" ]
then
echo "Target File or Directory already exists - $TO_DIR ";
exit 1
fi
mv "$FROM_DIR" "$TO_DIR"
STATUS=$?
if [ $STATUS -ne 0 ]
then
echo "Error in moving the file or directory";
echo "From directory : $FROM_DIR";
echo "To directory : $TO_DIR";
fi
}
fix_dirs ()
{
find_dirs "$1"|while read old_directory ; do
is_root_directory=`echo "old_directory" | grep -c "/"`;
if [[ $is_root_directory -ne 0 ]]
then
new_directory=$(pathname "$old_directory")/$(basename "$old_directory" | tr " " "_")
else
new_directory=$(echo "$old_directory" | tr " " "-")
fi
my_rename "${old_directory}" "${new_directory}"
done
}
fix_files ()
{
find_files "$1"|while read old_file ; do
new_file=$(pathname "$old_file")/$(basename "$old_file" | tr " " "-")
my_rename "${old_file}" "${new_file}"
done
}
WFILE=
WDIR=
DIR=
if [ "$#" -eq 0 ]
then
usage
fi
while [ $# -gt 0 ]
do
case $1 in
-d)
WDIR=1
;;
-f)
WFILE=1
;;
-*)
usage
;;
*)
if [ -d "$1" ]
then
DIR="$1"
else
print -u2 "$1 does not exist ..."
exit 1
fi
;;
esac
shift
done
if [ -z "$DIR" ]
then
echo "Please enter the directory";
exit 1
fi
if [ ${PWD} == "$DIR" ]
then
echo "Cannot perform rename on current directory";
exit 1
fi
if [ "$DIR" == "." -o "$DIR" == ".." ]
then
echo "cannot rename current or previous directory";
exit 1
fi
if [[ "$WDIR" == "" && "$WFILE" == "" ]] ; then
usage
exit 1
fi
if [ "$WDIR" -a "$WFILE" ]
then
fix_files "$DIR"
fix_dirs "$DIR"
elif [ "$WDIR" ]
then
fix_dirs "$DIR"
elif [ "$WFILE" ]
then
fix_files "$DIR"
fi

Running "ksh -n" on your example shows some possible problem on lines 70/71:
foo: warning: line 70: `...` obsolete, use $(...)
foo: warning: line 71: -ne within [[...]] obsolete, use ((...))
The relevant lines are
is_root_directory=`echo "old_directory" | grep -c "/"`;
if [[ $is_root_directory -ne 0 ]]
That was using a ksh93 on my Debian6, which likely is not the same version as the one you are using. However, you may find that making the suggested change to the second line will make your warning go away.

Thanks a lot for you information.
I tried bash similar to the ksh.
bash ./scriptname.sh
I corrected the errors. and below are the modified codes,
my_rename()
{
FROM_DIR="$1";
TO_DIR="$2";
if [ ! -w "${FROM_DIR##*/}" ]
then
echo "Directory/file ${FROM_DIR##*/} Not Writable";
exit 1
fi
if [ -d "$TO_DIR" -o -f "TO_DIR" ]
then
echo "Target File or Directory already exists - $TO_DIR ";
exit 1
fi
mv "$FROM_DIR" "$TO_DIR"
STATUS=$?
if [ $STATUS -ne 0 ]
then
echo "Error in moving the file or directory";
echo "From directory : $FROM_DIR";
echo "To directory : $TO_DIR";
fi
}
fix_dirs ()
{
find_dirs "$1"|while read old_directory ; do
is_root_directory=$(echo "$old_directory" | grep -c "/");
if [ $is_root_directory -ne 0 ]; then
new_directory=$(pathname "$old_directory")/$(basename "$old_directory" | tr " " "_")
else
new_directory=$(echo "$old_directory" | tr " " "-")
fi
my_rename "${old_directory}" "${new_directory}"
done
}

Related

sed command in Solaris server

I have many javascript files in my solaris server which have some debug, print and trace statements which I want to comment. There are hundreds of file like this.
I have found a script to do this but the problem is the script is removing the urhk_b2k_printRepos statement instead of commenting it. The script is as below:
if [ $# -ne 2 ]
then
echo "usage: prdel <script file name> <directory in which scripts are present>"
exit 1
fi
file=$1
dir=$2
if [ ! -s ${file} ]
then
echo "script file ${file} either does not exist or is empty (zero bytes)"
echo "Exiting..."
exit 1
fi
if [ ! -d ${dir} ]
then
echo "Invalid directory ${dir} entered"
echo "Exiting..."
exit 1
fi
cordir="./corrected"
prlogdir="./prlog"
if [ -d ${cordir} ]
then
echo "The corrected directory exist in the path, Please remove and run the tool again"
echo "Exiting..."
exit 1
else
mkdir ${cordir}
fi
if [ -d ${prlogdir} ]
then
echo "The prlog directory exist in the path, Please remove and run the tool again"
echo "Exiting..."
exit 1
else
mkdir ${prlogdir}
fi
errFile="$prlogdir/scr_err.rpt"
sucFile="$prlogdir/scr_suc.rpt"
Lines=`wc -l $file`
cntr=1
while [ $cntr -le $Lines ]
do
src=`head -$cntr $file|tail -1`
echo "$cntr. Processing $src...."
srcPath="${dir}/${src}"
if [ ! -s ${srcPath} ]
then
echo "Script file ${src} does not exist in the path given" >> ${errFile}
else
cp $srcPath $cordir/$src.tmp
srctemp="$cordir/$src.tmp"
srccor="$cordir/$src.corrected"
printcnt=`grep -ci "^[ ]*print" $srctemp`
if [ $printcnt -ne 0 ]
then
cat $srctemp|sed 's/^[ ]*[ ]*print[ ]*(/#print(/'|sed 's/^[ ]*[ ]*PRINT[ ]*(/#PRINT(/' > $srccor
mv $srccor $srctemp
fi
prreposcnt=`grep -ci "printrepos" $srctemp`
if [ $prreposcnt -ne 0 ]
then
cat $srctemp|sed 's/^.*urhk_b2k_printRepos.*/#Printrepos statement removed/'|sed 's/^.*urhk_B2k_PrintRepos.*/#Printrepos statement removed/'|sed 's/^.*urhk_B2k_printRepos.*/#Printrepos statement removed/'|sed 's/^.*urhk_b2k_PrintRepos.*/#Printrepos statement removed/' > $srccor
else
cp $srctemp $srccor
fi
echo "Script file $src correction is done" >> ${sucFile}
rm $srctemp
diff $srcPath $srccor >> $prlogdir/$src.diff.rpt
fi
cntr=`expr $cntr + 1`
done
echo "done"
I am completely new to shell scripting. Can anyone help me to modify this script to comment "urhk_b2k_printRepos" lines and also comment "TRACE ON" lines.

Bash script "Syntax Error: Unexpected end of file"

The goal is to create a simple trash utility using a Bourne shell (it's part of an assignment). I am receiving the following error: "line 17: Syntax Error: Unexpected end of file"
I have been staring at the code for a few hours now and I can't see the mistake (probably something simple I am overlooking)
#!/bin/sh
if [$# == 0] ;then
echo "Usage: trash -l | -p | { filename }*"
else
if $1 == '-l'; then
dir $HOME/.trash
else if $1=='-p'; then
rm $HOME/.trash/*
else
for i in ${} ;do
mv i $HOME/.trash
done
fi
fi
Thanks!
This is what I achieved using shellcheck:
#!/bin/sh
if [ $# -eq 0 ] ;then
echo "Usage: trash -l | -p | { filename }*"
else
if [ "$1" = '-l' ]; then
dir "$HOME"/.trash
elif "$1"=='-p'; then
rm "$HOME"/.trash/*
else
for i in ${} ;do
mv "$i" "$HOME"/.trash
done
fi

Bash script: Copies image files from source dir to destination dir and adds an extra suffix on files with same file name

I have this script that copies image files from source directory to destination directory. There are some image files in the source directory that have the same name but different file size. This script also compares the two files with the same name using a stat command. Now, I want to add a string suffix e.g. IMG0897.DUP.JPG before the file extension to the files with the same file name that are going to be copied over to the destination folder. At the moment, my script adds the file size of the file to the file name.
I need help on how to add a string of text of my own rather than the size of the file.
Here's my script:
#!/bin/sh
SEARCH="IMG_*.JPG"
SOURCE= $1
DEST=$2
test $# -ne 2 && echo Usage : phar image_path archive_path
if [ ! -e $1 ]
then echo Source folder does not exist
fi
if [ ! -e $2 ]
then mkdir $2/
fi
# Execute the script.
if [ "${SEARCH%% *}" = "$SEARCH" ]; then
command="find \"$1\" -name \"$SEARCH\""
else
command="find \"$1\" -name \"${SEARCH%% *}\""$(for i in ${SEARCH#* }; do echo -n " -o -name \"$i\""; done)
fi
# Run the main loop.
eval "$command" | while read file; do
bn=$(basename "$file")
bc=$(stat -c%s "$file")
if [ -f "${2}/$bn" ] && [ "$bc" -ne $(stat -c%s "${2}/$bn") ]; then
bn="$bn.$bc"
fi
if [ -f "${2}/$bn" ]; then
echo "File ${2}/$bn already exists."
else
echo "Copying $file to $2/$bn"
cp -a "$file" "$2/$bn"
fi
done
exit 0
else
echo "Error : Can't find $1 or $2"
exit 1
fi
I modified your scripte slightly.
#!/bin/sh
SEARCH="IMG_*.JPG"
SOURCE=$1
DEST=$2
SUFFIX=DUP
test $# -ne 2 && echo Usage : phar image_path archive_path
if [ ! -e $1 ]
then echo Source folder does not exist
fi
if [ ! -e $2 ]
then mkdir $2/
fi
# Execute the script.
if [ "${SEARCH%% *}" = "$SEARCH" ]; then
command="find \"$1\" -name \"$SEARCH\""
else
command="find \"$1\" -name \"${SEARCH%% *}\""$(for i in ${SEARCH#* }; do echo -n " -o -name \"$i\""; done)
fi
# Run the main loop.
eval "$command" | while read file; do
bn=$(basename "$file")
bc=$(stat -c%s "$file")
if [ -f "${2}/$bn" ] && [ "$bc" -ne $(stat -c%s "${2}/$bn") ]; then
bc=$(echo ${bn}|cut -d. -f2)
bn=$(echo ${bn}|cut -d. -f1)
bn=$bn.$SUFFIX.$bc**
fi
if [ -f "${2}/$bn" ]; then
echo "File ${2}/$bn already exists."
else
echo "Copying $file to $2/$bn"
cp -a "$file" "$2/$bn"
fi
done
exit 0
else
echo "Error : Can't find $1 or $2"
exit 1
fi
My execution result is:
root#precise32:/vagrant# sh JPG_moves.sh /root/dir1/ /root/destination/
Copying /root/dir1/IMG_0897.JPG to /root/destination//IMG_0897.JPG
root#precise32:/vagrant# sh JPG_moves.sh /root/dir2/ /root/destination/
Copying /root/dir2/IMG_0897.JPG to /root/destination//IMG_0897.DUP.JPG

Giving a parameter and help section to shell script

I have a problem that I want to determine folder name manually. For example, if user runs this script as below,
./scriptname -k
They can take the results, but folder names are determined in script. How can I determine folder name manually as below?
You could write the help as a function:
usage(){
cat <<EOT
Usage: ${0##*/} dir1 dir2
EOT
}
if [ $# -ne 2 ]; then
usage
echo 'please enter two folder names'
exit 1
fi
if [ ! -d "$1" ]; then
usage
echo "$1 is not a folder"
exit 1
fi
if [ ! -d "$2" ]; then
usage
echo "$2 is not a folder"
exit 1
fi
# continue script
#!/bin/bash
check_input_directories()
{
if [[ ! -f "${folder1}" "${folder2}" ]]; then
#your searching program
fi
}
Show_help()
{
cat <<_EOF
Usage : $0 <options> <path>
Options:
-f <folder> <folder>
_EOF
}
MAIN () {
case "$1" in
-f)
check_input_directories
;;
*)
Show_help
esac
}
MAIN $*
I deleted one untested answer yesterday and return today with this tested bash script:
#!/bin/bash
check_input_directories() {
folder1="$1"
folder2="$2"
if [[ ! -d "${folder1}" && ! -d "${folder2}" ]]; then
dir1=~/${folder1}
dir2=~/${folder2}
for i in $(find $dir1 -printf "%f\n")
do
find $dir2 -name $i -print
done
fi
}
Show_help() {
cat <<_EOF
Usage : $0 <options> <path>
You can use this script as below,
-d folder1/ folder2/
_EOF
}
MAIN () {
case "$1" in
-d)
check_input_directories $2 $3
;;
*)
Show_help
esac
}
MAIN $*

Check if passed argument is file or directory in Bash

I'm trying to write an extremely simple script in Ubuntu which would allow me to pass it either a filename or a directory, and be able to do something specific when it's a file, and something else when it's a directory. The problem I'm having is when the directory name, or probably files too, has spaces or other escapable characters are in the name.
Here's my basic code down below, and a couple tests.
#!/bin/bash
PASSED=$1
if [ -d "${PASSED}" ] ; then
echo "$PASSED is a directory";
else
if [ -f "${PASSED}" ]; then
echo "${PASSED} is a file";
else
echo "${PASSED} is not valid";
exit 1
fi
fi
And here's the output:
andy#server~ $ ./scripts/testmove.sh /home/andy/
/home/andy/ is a directory
andy#server~ $ ./scripts/testmove.sh /home/andy/blah.txt
/home/andy/blah.txt is a file
andy#server~ $ ./scripts/testmove.sh /home/andy/blah\ with\ a\ space.txt
/home/andy/blah with a space.txt is not valid
andy#server~ $ ./scripts/testmove.sh /home/andy\ with\ a\ space/
/home/andy with a space/ is not valid
All of those paths are valid, and exist.
That should work. I am not sure why it's failing. You're quoting your variables properly. What happens if you use this script with double [[ ]]?
if [[ -d $PASSED ]]; then
echo "$PASSED is a directory"
elif [[ -f $PASSED ]]; then
echo "$PASSED is a file"
else
echo "$PASSED is not valid"
exit 1
fi
Double square brackets is a bash extension to [ ]. It doesn't require variables to be quoted, not even if they contain spaces.
Also worth trying: -e to test if a path exists without testing what type of file it is.
At least write the code without the bushy tree:
#!/bin/bash
PASSED=$1
if [ -d "${PASSED}" ]
then echo "${PASSED} is a directory";
elif [ -f "${PASSED}" ]
then echo "${PASSED} is a file";
else echo "${PASSED} is not valid";
exit 1
fi
When I put that into a file "xx.sh" and create a file "xx sh", and run it, I get:
$ cp /dev/null "xx sh"
$ for file in . xx*; do sh "$file"; done
. is a directory
xx sh is a file
xx.sh is a file
$
Given that you are having problems, you should debug the script by adding:
ls -ld "${PASSED}"
This will show you what ls thinks about the names you pass the script.
Using -f and -d switches on /bin/test:
F_NAME="${1}"
if test -f "${F_NAME}"
then
echo "${F_NAME} is a file"
elif test -d "${F_NAME}"
then
echo "${F_NAME} is a directory"
else
echo "${F_NAME} is not valid"
fi
Using the "file" command may be useful for this:
#!/bin/bash
check_file(){
if [ -z "${1}" ] ;then
echo "Please input something"
return;
fi
f="${1}"
result="$(file $f)"
if [[ $result == *"cannot open"* ]] ;then
echo "NO FILE FOUND ($result) ";
elif [[ $result == *"directory"* ]] ;then
echo "DIRECTORY FOUND ($result) ";
else
echo "FILE FOUND ($result) ";
fi
}
check_file "${1}"
Output examples :
$ ./f.bash login
DIRECTORY FOUND (login: directory)
$ ./f.bash ldasdas
NO FILE FOUND (ldasdas: cannot open `ldasdas' (No such file or directory))
$ ./f.bash evil.php
FILE FOUND (evil.php: PHP script, ASCII text)
FYI: the answers above work but you can use -s to help in weird situations by checking for a valid file first:
#!/bin/bash
check_file(){
local file="${1}"
[[ -s "${file}" ]] || { echo "is not valid"; return; }
[[ -d "${file}" ]] && { echo "is a directory"; return; }
[[ -f "${file}" ]] && { echo "is a file"; return; }
}
check_file ${1}
Using stat
function delete_dir () {
type="$(stat --printf=%F "$1")"
if [ $? -ne 0 ]; then
echo "$1 directory does not exist. Nothing to delete."
elif [ "$type" == "regular file" ]; then
echo "$1 is a file, not a directory."
exit 1
elif [ "$type" == "directory" ]; then
echo "Deleting $1 directory."
rm -r "$1"
fi
}
function delete_file () {
type="$(stat --printf=%F "$1")"
if [ $? -ne 0 ]; then
echo "$1 file does not exist. Nothing to delete."
elif [ "$type" == "directory" ]; then
echo "$1 is a regular file, not a directory."
exit 1
elif [ "$type" == "regular file" ]; then
echo "Deleting $1 regular file."
rm "$1"
fi
}
https://linux.die.net/man/2/stat
https://en.m.wikipedia.org/wiki/Unix_file_types
A more elegant solution
echo "Enter the file name"
read x
if [ -f $x ]
then
echo "This is a regular file"
else
echo "This is a directory"
fi
Answer based on the title:
Check if passed argument is file or directory in Bash
This works also if the provided argument has a trailing slash .e.g. dirname/
die() { echo $* 1>&2; exit 1; }
# This is to remove the the slash at the end: dirName/ -> dirName
fileOrDir=$(basename "$1")
( [ -d "$fileOrDir" ] || [ -f "$fileOrDir" ] ) && die "file or directory $fileOrDir already exists"
Testing:
mkdir mydir
touch myfile
command dirName
# file or directory mydir already exists
command dirName/
# file or directory mydir already exists
command filename
# file or directory myfile already exists
#!/bin/bash
echo "Please Enter a file name :"
read filename
if test -f $filename
then
echo "this is a file"
else
echo "this is not a file"
fi
One liner
touch bob; test -d bob && echo 'dir' || (test -f bob && echo 'file')
result is true (0)(dir) or true (0)(file) or false (1)(neither)
This should work:
#!/bin/bash
echo "Enter your Path:"
read a
if [[ -d $a ]]; then
echo "$a is a Dir"
elif [[ -f $a ]]; then
echo "$a is the File"
else
echo "Invalid path"
fi

Resources