Jump to content

shell scripting Help Please


MutaMestri

Recommended Posts

DB Unix Experts , Small help please

I have variable with bunch of filenames few with spaces and few non-spaces , please see below , when file executing with space , it should take it as it is.

Script

=====

files="
DKC Lab
postcr
"

for file in $files; do
    echo "$file"
done

 

 

Output

====

$ sh while.sh


DKC          ===> Here the output i want to be same as   DKC Lab
Lab
postcr
 

 

 

Please help bro

Link to comment
Share on other sites

Then you should change the script to the below  or 

1) Files='DKC Lab 

               postcr

echo $Files.

 

2) don't use any script use echo "what you want" 😝.  If you have automated process add subject "job completed with no issues"

Link to comment
Share on other sites

1 hour ago, Kumaravaarma said:

Then you should change the script to the below  or 

1) Files='DKC Lab 

               postcr

echo $Files.

 

2) don't use any script use echo "what you want" 😝.  If you have automated process add subject "job completed with no issues"

for loop considers "spaces" as a seperator for input. Your file "DKC Lab" has space in it. Can't you just rename such files? 

Link to comment
Share on other sites

11 minutes ago, kumar4world said:

for loop considers "spaces" as a seperator for input. Your file "DKC Lab" has space in it. Can't you just rename such files? 

sorry just add this to your script at the top "IFS=$'\n'" and put all the files in a temp file (temp_file.txt) and run 

 

your temp file containing file names:

cat temp_file.txt
DKC Lab
postcr

your script:

IFS=$'\n'
for file in `cat temp_file.txt`; do echo $file;done
DKC Lab
postcr

 

Link to comment
Share on other sites

1 hour ago, kumar4world said:

sorry just add this to your script at the top "IFS=$'\n'" and put all the files in a temp file (temp_file.txt) and run 

 


your temp file containing file names:

cat temp_file.txt
DKC Lab
postcr

your script:

IFS=$'\n'
for file in `cat temp_file.txt`; do echo $file;done
DKC Lab
postcr

 

Can you please one with an example for both while and for loop and let how to use the input parameters

Link to comment
Share on other sites

1 hour ago, Kumaravaarma said:

Can you please one with an example for both while and for loop and let how to use the input parameters

file:

cat temp_file.txt

DKC Lab
postcr

while

 

while  read -r LINE ; do echo "${LINE}"; done < temp_file.txt
DKC Lab
postcr



for:
 

IFS=$'\n'
for file in `cat temp_file.txt`; do echo $file;done
DKC Lab
postcr

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...