I believe this is a Bash basic question... I am bit ashamed for asking actually...
I want to create a Bash script that compares 2 different folders:
1) work_folder
and
2) work_folder.git
#!/bin/bash
FOLDER_NAME=`pwd | awk -F/ '{ print $NF }' | awk -F. '{ print $1 }'`
printf "DEBUG: VAR FOLDER_NAME=%s\n" "$FOLDER_NAME"
LIST_OF_FILE_SRC=`ls -lht ../"$FOLDER_NAME | grep "^-" | awk '{ print $NF }'`
printf "DEBUG: VAR LIST_OF_FILE_SRC=%s\n" "$LIST_OF_FILE_SRC"
for FILE in "$LIST_OF_FILE_SRC" ;
do
printf "DEBUG: VAR FILE=%s\n" "$FILE"
printf "another file inside the FOR loop\n:"
done;
Unfortunately inside the FOR loop I do not get a the list of file ONE by ONE...
I wish to do some treatments and some git auto commit in the git folder tracking the work folder.
But my problem at this stage would be resolved if:
Anyone could explain to me how to retrieve the list of file, file by file?
Many thanks for your help and keep up the good work!
This is one of those rare cases where you do NOT want quotes. Change:
for FILE in "$LIST_OF_FILE_SRC" ;
to:
for FILE in $LIST_OF_FILE_SRC
But, of course, this won't work if a file in your list of files contains any whitespace characters (but if that was a problem, your awk print $NF statements wouldn't work either).
That is great!
To avoid trouble with some file names with a spaces.
How would you test if a file name has a space?
How would you rename the file with the same name but replacing each space character with a "_"?
I wouldn't test for filenames containing spaces, tabs, or newlines, I'd just write code that works even if they are present. (And if I found users creating filenames containing whitespace characters, I'd ban them from using my system! Unfortunately, there need to be exceptions for some things like .mp3 and .mp4 files with spaces in the titles of songs and movies, but most users creating filenames containing spaces are just trying to create problems for inexperienced programmers.)
Stop trying to create lists and then trying to process the list. Process filenames one at a time. And, learn to use shell built-ins instead of pipelines of awk , grep , and ls commands. Try something more like:
#!/bin/bash
# Assume that we are starting in a directory with a filename suffix ".git".
# Get related non-".git" directory name.
FOLDER_NAME=${PWD##*/} # Extract last component of current working directory
FOLDER_NAME=${FOLDER_NAME%.git} # Throw away ".git".
printf 'DEBUG: VAR FOLDER_NAME=%s\n' "$FOLDER_NAME"
# Process the files in the non-".git" directory...
for FILE in ../"$FOLDER_NAME"/*
do # Skip files that are not regular files...
[ ! -f "$FILE" ] && continue
# Do whatever you want with the regular file from ../"$FOLDER_NAME"...
printf 'DEBUG: VAR FILE="%s"\n' "$FILE"
printf 'another file inside the FOR loop\n:' # Should ":" be before newline?
done
If you don't understand how something in the above works, try running it with:
bash -xv script_name
and watch how bash processes each command in the script. If you still don't understand, ask questions... We're here to help you learn.
Any other good reading beyond your answers to learn how to properly code in Bash?
A tutorial book?
Some classical script examples in a book, a website, or maybe a reference developer on github or Code Snippets - Snipplr Social Snippet Repository or something else?
Some other forums maybe too?
If some answers or advice cannot be given publicly, please drop me a quick message.
Many many thanks for your help!
Kindest regards,
Freddie
If you find my suggestions informative, you can search this site for my 7,000+ posts.
I'm old fashioned and taught myself how to use UNIX utilities and the C programming language by reading a PWB UNIX manual from cover to cover three or four times making sample scripts and programs that used utility options I had never noticed before, used C functions I'd never heard of before, etc., until I figured out how they worked (or found someone with more experience than me to explain it to me). When I found bugs in the man pages, I filed bug reports and suggested better wording and examples. Eventually they started asking me to review man page changes new man pages for new functions and utilities. (But, of course, that was well before there were on-line tutorials or a long list of good books. And, hard copy manuals are harder to find now.)
To learn how to use bash on your system, I would still strongly suggest that you read the bash man page on your system very carefully. At first, some parts of it may look cryptic and incomprehensible, but with a little practice reading other man pages, you'll find a wealth of information there.
To write code that will work on a wide variety of BSD, Linux, and UNIX systems, look at the man pages on this site and select the POSIX 1003.1 man page set. If you restrict yourself to the utilities and utility options described there, most of the code you write will work on most systems.