if ! cd "${1:-no-such-directory}" # also quote on the off chance that something in the path has spaces
then
echo "abort: could not switch to '$1' or parameter was missing"
exit 1
fi
ls | while read f # for * doesn't handle filenames with spaces or large numbers of files.
If you want to ensure that the script has been given a viable working directory, then $1 should be tested to confirm that it's non-empty and a directory. In that instance, blindly substituting a string in that way can be dangerous.
You are mistaken about the for f in * ... construct. Since pathname expansion occurs after field splitting, the expansion of * is absolutely safe with regard to IFS characters (by default, space, tab, and newline).
Ironically, the statement you replaced it with does not handle spaces correctly in all cases. If ls prints a filename with leading or trailing spaces, read will discard those, yielding either a non-existent file or a different file. Further, if the filename ended with a backslash, that backslash would be stripped and the next filename in the list, if any, would be appended. while IFS= read -r f fixes both issues.
Demonstration:
Code:
$ touch 'a1\' a2 ' spaces '
$ # CORRECT RESULT
$ for f in *; do printf ':%s:\n' "$f"; done
:a1\:
:a2:
: spaces :
$ # ERRONEOUS HANDLING OF LEADING/TRAILING WHITESPACE AND TRAILING BACKSLASH
$ ls | while read f; do printf ':%s:\n' "$f"; done
:a1a2:
:spaces:
$ # FIX ONLY THE BACKSLASH
$ ls | while read -r f; do printf ':%s:\n' "$f"; done
:a1\:
:a2:
:spaces:
$ # FIX ONLY THE SPACES
$ ls | while IFS= read f; do printf ':%s:\n' "$f"; done
:a1a2:
: spaces :
$ # FIX BOTH
$ ls | while IFS= read -r f; do printf ':%s:\n' "$f"; done
:a1\:
:a2:
: spaces :
You are correct in that there may be a limit to how many files the pathname expansion in the for-loop list can handle, but if it's sufficient (usually is) for the task at hand, it's the simplest and safest method.
I would like to have a script that would change my current working directory. However, any time I execute a 'cd' command in a script, it holds only for the life of that script -- the working directory on exit is the same as when the script was initiated. Is it possible to have the script return... (3 Replies)
:confused:
Hi All,
This script is not working. I want to change the directory as per users selection in current shell. Looks like it is spawning sub-shell internally.
I have used
. changedir.sh
source changedir.sh
./changedire.sh
But did not work. Currently shell directory remain the... (4 Replies)
Hi,
I'm struggling to write a script to do the following,
-will go through each line in the file
-in a specific character positions, changes
the value to a new value
-These character positions are fixed througout the file
-----------------------
e.g.: file1.sh will have the following 3... (4 Replies)
Hi,
I am trying to change the directory in my script, & want to check if the directory is present or not .
Ex:
cd /home/xyz/temp/logs
if the logs directory is not present i want to show the error in script instead of shell script error.
Can anybody please help me on the same
Thx in... (2 Replies)
HI,
I need to change the working directory by using the shell script
/Export/home/user_name
I have to go one step back like
/Export/home
Please help on this.:confused: (3 Replies)
Hi All,
I am using the awk command to replace ',' by '\t' (tabs) in a csv file. I would like to apply this to all .csv files in a directory and create .txt files with the tabs.
How would I do this in a script?
I have the following script called "csvtabs":
awk 'BEGIN {
FS... (4 Replies)