Shell Script to Abort if file name has space.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to Abort if file name has space.
# 1  
Old 04-03-2013
Shell Script to Abort if file name has space.

Hi,
In my shell script I use the following code to copy files from one directory to other.

Code:
 
    for file in `ls`; do
 
    #----------------- Copy Files From INDIR to OUTDIR  -------------#
        echoen "Copying File ${INDIR}/$file to ${OUTDIR}/${file}"
        cp ${INDIR}/$file  ${OUTDIR}/$file
        if [[ $? == 0 ]]; then
            echoen "Copy Successful ${INDIR}/$file to ${OUTDIR}/$file!!"
        else
            echoen "Could not copy file ${INDIR}/$file to ${OUTDIR}/$file"
            exit -15
        fi
 
    done


I would like to add logic that before copy happens I would like to check if the file name has any spaces in it. If it has space then I would like to abort the process.
The reason why I want to abort is in next part of shell script I remove/delete files based on looping on output of ls command and having a space might create unneccessary problems.

Help is appreciated to modify the above code to abort if space is found in the file name.
# 2  
Old 04-03-2013
Add this code before the copy:
Code:
if [[ "$file" =~ \ |\' ]]
then
   exit -15
fi

# 3  
Old 04-03-2013
Your script is going to blow up on files with spaces in them because of the `ls` but it doesn't have to be that way. Backticks split upon spaces, not lines.

Try
Code:
for file in *

which avoids that problem completely. You will get complete, unsplit filenames. Just be sure to quote all your variables inside double-quotes when you use them and you'll be fine.

Last edited by Corona688; 04-03-2013 at 12:36 PM..
# 4  
Old 04-03-2013
Quote:
Originally Posted by Corona688
Your script is going to blow up on files with spaces in them because of the `ls` but it doesn't have to be that way. Backticks split upon spaces, not lines.

Try
Code:
for file in *

which avoids that problem completely. You will get complete, unsplit filenames. Just be sure to quote all your variables inside double-quotes when you use them and you'll be fine.

Could you please help me understand how this
Code:
*

is doing same as
Code:
`ls`

Appreciate your help.
# 5  
Old 04-04-2013
My point is they're not the same.

`ls` means "run the ls command, and split apart its output on all whitespace". You can get away with this when your filenames have no spaces in them, but if they do, that's a problem.

* means 'generate a list of all files in the current folder'. It's not part of ls, it's part of the shell, meaning it works nearly everywhere. Type echo * into your shell and see what it does.

You can use it in more complicated ways. A* would be 'all files beginning with an uppercase letter A', *.txt would be "all files ending in the extension .txt", and so forth. You can even do things like /path/to/*/file, to find a file named 'file' inside any folder inside /path/to/.

It won't split on spaces, only on filenames.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Abort the execution if one script have errors

Gents, I have a script which will run others scripts . Example #!/bin/bash script1 script2 script3 script4 I would like to stop all process if any of the script got errors?. If for example script1 works fine will continue script2, then if the script2 got errors, The bash should... (2 Replies)
Discussion started by: jiam912
2 Replies

2. Shell Programming and Scripting

Awk. Abort script if condition was met.

I want to abort script if input variable matched first field in any line of a file. #!/bin/sh read INPUTVAR1 awk "{if(\$INPUTVAR1 == $1) x = 1} END {if(x==1) print \"I want to abort script here\"; else print \"OK\"}" /etc/some.conf I tried "exit" and system("exit") but no luck. (1 Reply)
Discussion started by: urello
1 Replies

3. Shell Programming and Scripting

Remove Space and blank line from file in UNIX shell script

I have below file. I want to remove space at begining of every line and then after also remove blank line from file. I use below code for each operation. sed -e 's/^*//' < check.txt > check1.txt sed '/^\s*$/d' < check1.txt > check2.txt above code not remove all the space... (12 Replies)
Discussion started by: Mohin Jain
12 Replies

4. Shell Programming and Scripting

How to Abort or Come out of ksh script?

Hi All, I have a requirement where if Source Count is not equal to Target count, then I need to exit/abort the script and come out. Then send an email to thummi@email.com saying "RECORD COUNT DOES NOT MATCH. Please need your help. Here is what I have written : If even the count is not... (1 Reply)
Discussion started by: thummi9090
1 Replies

5. Solaris

Abort the shell script if any hive sql query gets failed

Below is my shell script from which I am trying to invoke few Hive SQL queries and the below shell script works fine. Problem Statement:- If you see my first `hive -e` block in the below shell script which contains a very long Hive SQL query. Sometimes that Hive SQL query gets failed due to... (1 Reply)
Discussion started by: raihan26
1 Replies

6. Shell Programming and Scripting

Blank Space is not appending in each row of CSV File - Shell Script

I am calling SQL script in my UNIX Shell script and trying to create the CSV file and my last column value of each row is 23 blank spaces. In my SQL script,the last column is like below. RPAD(' ',23,' ') -- Padding 23 blank Spaces The CSV file is generated but the sapce(23 spaces) is... (2 Replies)
Discussion started by: praka
2 Replies

7. Shell Programming and Scripting

Problem in checking file abort

Hi, I would like to know given executing a file with inputs, I would like to know when does it terminate /abort abnormally. I tried to append an echo $? after executing my program which is in C. However, there is nothing..It shows 0 even though the program actually exit. my command is... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

8. UNIX for Advanced & Expert Users

examine core file after abort()

I'm running Red Hat Linux 2.6.7 on a x86_64 box. I have a core file from a program that called abort(). Does anyone here know how can I get a backtrace? (Re-creating the error with svd running inside gdb has proved impossible). % gdb svd core.25223 GNU gdb Red Hat Linux... (2 Replies)
Discussion started by: svact
2 Replies

9. Shell Programming and Scripting

How to make bash script abort?

I have a little bash script that includes make to compile with g++ and then a statement to actually run the compiled program. When it (the script) gets a syntax error, it does not abort and continues to run the previous version of the program. How can I make the script abort when g++ generates a... (1 Reply)
Discussion started by: siegfried
1 Replies
Login or Register to Ask a Question