Long file names within shell script


 
Thread Tools Search this Thread
Operating Systems SCO Long file names within shell script
# 1  
Old 12-07-2016
Long file names within shell script

I am downloading a zip file that contain files that are very long. I am trying to process them, but cannot. I can move the files from one directory to another at the shell prompt, but not within a shell script, I get a stat error.

The files look somewhat like this;

Code:
08fe457ab_34fed678_MCRXCOMM.FIL_999.edi

I do not know what the problem is.

Any help would be appreciated!

---------- Post updated at 08:51 PM ---------- Previous update was at 07:59 PM ----------

I was able to resolve this problem.

I was trying to delete the original post, but did not know how.

Last edited by Scrutinizer; 12-07-2016 at 11:09 PM..
# 2  
Old 12-08-2016
Quote:
Originally Posted by trolley
I was trying to delete the original post, but did not know how.
Don't! Write your solution here instead, so that others, which have the same (or a similar) problem can learn from your findings.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 12-08-2016
Not sure how I resolve my issue. I was trying many things and it just finally worked. I was using the following code;

Code:
mv $filxfr/*.edi  $filtmp/.
for RPTFIL in `ls`
do
    mv $RPTFIL FLXRPT.DAT
    rpt999
       .
       .
       .
done

In the filxfr directory, I had zip and text files. So the above code would not work. I made sure the text files were in another directory for processing. I later found an error in my subroutine (ie. rpt999) that was not looking in the correct directory.

Again, I am not sure this helps. I would have posted this last night, but I thought it would not be helpful. That is why I wanted to delete my original post. I am a firm believer in sharing results or ideas.
# 4  
Old 12-08-2016
"

Quote:
Originally Posted by trolley
Code:
mv $filxfr/*.edi  $filtmp/.
for RPTFIL in `ls`
do
    mv $RPTFIL FLXRPT.DAT
    rpt999
       .
       .
       .
done

First off: thanks for sharing. And because you did that (instead of just saying you have a solution) you get something back immediately in return: some explanation why your script probably hasn't worked. So this will be at least helpful to you (but perhaps some others, because what you did is a common error). Here it goes:

When you write scripts, as a rule of thumb, NEVER use relative paths or filenames. See, you can call a script from everywhere and its workings should always be the same, regardless of from where you called it. The line

Code:
for RPTFIL in `ls`

will work if you call the script in one directory, but fail if you call it in another. Save for that, you should avoid the backticks (they are obsolete) but you do not need them anyway: for file in *edi ; do will work the same and you do not need to call ls for that. You see, "*" means already "all files" and is interpreted b the shell, not the program called. A command like:

Code:
ls *file

is processed this way: first, the shell sees the asterisk and "expands" it to a list of files matching, in this case: all files with names ending in "file" in the current directory. Then the result of this "expansion" is put on the (intermediate) command line:

Code:
ls *file                     # your command
ls a.file b.file c.file      # after expansion (you won't see that)

Finally the command itself (here ls) is called and given the list of files, which it displays. If you call ls without any parameters it doesn't do much at all, only when you use additional options it gets more and more useful. But try, to test what i have said, on the commandline:

Code:
echo *

and you will see that it produces the same list as ls without options. And with the explanation above you know why.

Anyway, preferable is to provide what the script needs to know on the commandline, like:

Code:
myscript.sh /path/to/my/data

UNIX has a quite fixed directory hierarchy and there is a certain place for all sorts of things. You put i.e. temporary files always in /tmp and, on the other hand, whatever is in /tmp can be deleted without question. So your script can assume some paths to be given.

Second: as a rule of thumb, always quote your variables! The shell treats space as separators, so a line like:

Code:
a b c

will call a program "a" with the first parameter "b" and the second parameter "c". It is possible to name a program "a b" or pass parameters with enclosed spaces you need to quote:

Code:
"a b" "c d"    # will start a program "a b" with a single parameter "c d"

Now let us see your script itself:

The first thing is: when you start programming, keep a strict order. It will help you to organize your code. We always start with a shebang line, telling the OS which shell to use (instead of relying on an arbitrary default that could change), then declare the variables we use. This gives us the opprtunity to write in commentary what should go into these variables. Note that everything after "#" is a commentary.

Code:
#! /bin/ksh
# maybe you want to use "#! /bin/bash" instead, this line is called "shebang"

dir="$1"                     # working directory
tmpdir="/tmp/mydir"          # temporary workspace
file=""                      # filename buffer

if [ -e "$tmpdir" ] ; then
     mkdir -p "$tmpdir"
fi

mv ${dir}/*edi  "$tmpdir"

for file in "$tmpdir/*" ; do
    mv "file" "/some/place/FLXRPT.DAT"
    process_file
    if [ $? -ne 0 ] ; then         # you might want some error reporting here
        echo "ERROR processing $file" >2&
    fi
    ....
done

rmdir "$tmpdir"

exit 0

This will first copy all files "*edi" from some directory to "/tmp/mydir", then copy one file after other to the name "/some/place/FLXRPT.DAT" and process it. if the (hypothetical) command "process_file" exits with any other value than 0 (conventionally this means success) an error message is produced and the script continues (it might also stop if you put an "exit" command after the echo). Once the list is finished the temporary directory is deleted and the script exits with 0 itself.

Now, i suppose this is not exactly what you wanted to do and you could (and should) modify it to better suit your needs. It is not so much about how to write a certain script but to show the princinples of how to write any script. Always bear in mind that scriptingis like any other form of software engineering and the same principles apply. In fact it is even more demanding (mostly on self-control) because you can get away initially with a lot of slack where other programming languages are more demanding from the start. You should mostly not rely on these shortcuts, because the longer your scripts get (and, believe me, once you get the hang of it you are NEVER going to stop) the more you need to drop these bad habits and do it the correct way. So better start developing good habits from the start.

In either case, if you have troubles extending your script we will be here to help you.

I hope this helps.

bakunin

Last edited by bakunin; 12-08-2016 at 02:59 PM..
This User Gave Thanks to bakunin For This Post:
# 5  
Old 02-15-2017
the shebang line needs to be corrected, do not place a space between exclamation mark and slash, type it like this
Code:
 
 #!/bin/ksh

This User Gave Thanks to migurus For This Post:
# 6  
Old 02-23-2017
Quote:
Originally Posted by migurus
the shebang line needs to be corrected, do not place a space between exclamation mark and slash, type it like this
Code:
 
 #!/bin/ksh

Sorry, but this is not correct: in fact both shebang lines:

Code:
#! /path/to/interpreter

and
Code:
#!/path/to/interpreter

will work because the three-byte "magic number" "#!/" as well as the 4-byte magic number "#! /" is built into the UNIX-kernel. You need to use absolute pathes, though, and i.e.

Code:
#! ../path/to/interpreter

will not work, regardless of a space being there or not.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

script regarding listing long group names

Hello, When listing the file systems (using ls -ltr) , if the group names are longer the group name is getting truncated. Can someone help with the script which would display the truncated group name? I appreciate if someone could help in this regard. (1 Reply)
Discussion started by: mike12
1 Replies

2. Shell Programming and Scripting

Find and rename long file names (html)

Hi Guys, I need a help. I have 1130 zip files. Each one of them has files including 1 html file with long file name (includes special charactors, Alphabetic and numbers). I have copied all 1130 zip files to my linux system and extracted using below command. Find . -name "*.zip" -exec... (7 Replies)
Discussion started by: Rajmani
7 Replies

3. Shell Programming and Scripting

want only file names (not whole path) in shell script

hi i wrote following script, #!/usr/bin/sh for index in `ls /tmp/common/*.txt` do echo "$index" done here index is giving full path but in my program i want only file names (not along with whole path) Eg. if in /tmp/common files are a.txt and b.txt den out should be a.txt b.txt ... (6 Replies)
Discussion started by: crackthehit007
6 Replies

4. Shell Programming and Scripting

Renaming file names in a shell script

I want to write a shell script that will rename all the file names to today's date attached to it.. so for example i have a file names like file1.sales.20081201.txt.c zbrs.salestxtn.20091101.txt.inn then it will rename both the files with todays date to it so the file names get changed... (1 Reply)
Discussion started by: rudoraj
1 Replies

5. Shell Programming and Scripting

Dealing with spaces in file names in a shell script

Hi, What's the best way to find all files under a directory - including ones with space - in order to apply a command to each of them. For instance I want get a list of files under a directory and generate a checksum for each file. Here's the csh script: #!/bin/csh set files = `find $1... (5 Replies)
Discussion started by: same1290
5 Replies

6. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies

7. UNIX for Dummies Questions & Answers

Displaying VERY long process names with ps -ef

Hi, I'm trying to display all process on an AIX server with the string SLRServer in them. Normally "ps -ef|grep SLRServer" would be sufficient, however in this instance the process name is enormous and the part which contains this string has been truncated, as you can see in the example below ... (8 Replies)
Discussion started by: m223464
8 Replies

8. Solaris

mkisofs and long file names

Well im using mkisofs to create iso images in Solaris 10 and then i use cdrw -i to burn the images to the cd the problem which i noticed recent ( im new to mkisofs) i noticed its break the long file names making them shorter i searched in its help and i find two parameters one -l and one... (2 Replies)
Discussion started by: XP_2600
2 Replies

9. Windows & DOS: Issues & Discussions

Dos window, long file names prob

At the dos command prompt, does anyone remember how to make it recognize long file names? ie, windows long file names for folders, my documents, if i'm at the :> prompt and want to change to that directory, how do i make it skip the space? I've tried cd "my document" cd my\documents cd 'my... (4 Replies)
Discussion started by: kymberm
4 Replies

10. UNIX for Dummies Questions & Answers

linux question, pardon, on long file names

Sorry, hope no one minds the linux question here, I use both unix and linux, and have come across a problem. On my linux box i have a dual boot, and i've set it up so i can access my windows data partition while in linux mode (mount the vfat partition), but linux doesn't recognize long file names,... (2 Replies)
Discussion started by: kymberm
2 Replies
Login or Register to Ask a Question