Ls command in a for loop displaying error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ls command in a for loop displaying error
# 1  
Old 01-13-2019
Ls command in a for loop displaying error

I have a few files inside a directory and in my code i am listing the files to get the filenames and do further operations.Details are given below:
Dirname is taken as argument in $3 and filenames in variable $file

Code:
Directory name =$3= /tmp/to-be_parsed/input
Filename= $file= JAN_DAT_TES1_201807181400.csv
                 J       JAN_DAT_TES2_201807181400.csv

The code i wrote is
Code:
for i in `ls $3/$file|cut -d'/' -f5`
do
mv ......
........
..........
done

When i run the script,it says
Code:
ls: /tmp/to-be_parsed/input/JAN_DAT_TES1_201807181400.csv:No such file or directory
ls: /tmp/to-be_parsed/input/JAN_DAT_TES2_201807181400.csv:No such file or directory

Files are present in the directory.I am not able to figure out why the ls command says no files.Could you pl. help me with this to correct the mistake
# 2  
Old 01-13-2019
Look like your count is wrong (I may be wrong still half asleep...) I count 4 => so it should be -f4
# 3  
Old 01-13-2019
Quote:
Originally Posted by vbe
Look like your count is wrong (I may be wrong still half asleep...) I count 4 => so it should be -f4
Thank you. I changed it to -f4 but still it shows the same error Smilie
# 4  
Old 01-13-2019
it only works with field 5
Code:
for i in `ls $3/$file|cut -d'/' -f5`

do so
Code:
for i in $(ls $3/$file | xargs basename -a)

--- Post updated at 10:30 ---

I think you have not correctly defined the variable $file. Try this
Code:
echo $3/$file


Last edited by nezabudka; 01-13-2019 at 07:26 AM..
# 5  
Old 01-13-2019
Quote:
Originally Posted by nezabudka
do so
Code:
for i in $(ls $3/$file | xargs basename -a)

much better indeed but why use any external utility when the shell can do the same:

Code:
for i in $(ls "${3}/${file}") ; do
     i="${i##*/}"

The only way this is going to break is if the filename has newlines embedded.

I hope this helps.

bakunin
These 3 Users Gave Thanks to bakunin For This Post:
# 6  
Old 01-13-2019
I would also replace this construction
Code:
for i in ${3}/$file ; do
     i="${i##*/}"

These 3 Users Gave Thanks to nezabudka For This Post:
# 7  
Old 01-14-2019
I wonder what the purpose of the loop is.
Do you want to search and loop over the found files?
Then use a glob search! For example
Code:
for pfn in "$3"/JAN_DAT_TES*.csv
do
  # the shell puts the glob pattern if there was no match, so ensure that the file exists
  [ -f "$pfn" ] || continue
  # fn=basename(pfn) achieved with a builtin modifier
  fn=${pfn##*/}
  echo "do something with $pfn or $fn"
done


Last edited by MadeInGermany; 01-14-2019 at 08:04 AM..
These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Displaying multiple variables in for loop

Hi! I've run into a problem where my variables are displayed in the wrong order. Basically I'm supposed to use a file that has information like this username:firstname:lastname:etc:etc. What I'm interested in doing is reformating it into a something more like this: username lastname,... (2 Replies)
Discussion started by: reindeermountai
2 Replies

2. Shell Programming and Scripting

Problem while displaying(cat) file content inside telnet loop .

Hi Team, Not getting the file output inside my email which i am sending from unix box. . Please refer the below code : #!/bin/sh { sleep 5 echo ehlo 10.56.185.13 sleep 3 echo mail from: oraairtel@CNDBMUREAPZP02.localdomain sleep 3 echo rcpt to: saurabhtripathi@anniksystems.com... (1 Reply)
Discussion started by: tripathi1990
1 Replies

3. Red Hat

Displaying command return in one line

Hello all I have a query (SQL) that returns a rather long field from an Oracle database. The field in question is defined on 400 characters but all these 400 cannot be displayed by the echo command. Thus when I launch the following command: echo "SELECT FIELD01 FROM TABLE_NAME;" | sqlplus -s... (9 Replies)
Discussion started by: S. BASU
9 Replies

4. Shell Programming and Scripting

[Solved] While loop error when add sqlplus command

Hi gurus, I hit a block when write the script. I need do while loop, in the loop I have code below. sqlplus -s abc/abc@abc <<EOF spool testwhile select * from dual; spool off exit; EOF when I run script with only this code, it works fine. if I add code as below: #!/bin/ksh ... (5 Replies)
Discussion started by: ken6503
5 Replies

5. Shell Programming and Scripting

for loop with internal unix command in statement throwing error

Hi I've gotten a plugin script that won't run. I keeps throwing an error at the following line. for BARCODE_LINE in `cat ${TSP_FILEPATH_BARCODE_TXT} | grep "^barcode"` do #something done The error reads ... (3 Replies)
Discussion started by: jdilts
3 Replies

6. Shell Programming and Scripting

while loop error. (command not found)

can any1 please tell me what is problem with following code: i=1; cat test| while read CMD; do Var$i=$CMD; or Var$i=$(echo $CMD) ; let i++ doneI keep getting error : line 4: Var1=sometext: command not found (2 Replies)
Discussion started by: kashif.live
2 Replies

7. UNIX for Dummies Questions & Answers

Trouble displaying parameters passed into a for loop

#!/bin/bash function check_num_args() { if ; then echo "Please provide a file name" else treat_as_file $* fi } function treat_as_file() { numFiles=$# for((i=1;i<=$numFiles;i++));do echo $i ... (3 Replies)
Discussion started by: kikilahooch
3 Replies

8. Shell Programming and Scripting

Displaying list of backup files using for loop

Hi, I want to display list of last 10 backup files (with numbering) from the temporary file c:/tmp/tmp_list_bkp.txt Example : 1) backup_file1 2) backup_file2 3) backup_file3 ........ ........ I tried as below but not working. for file in c:/tmp/tmp_list_bkp.txt do echo... (3 Replies)
Discussion started by: milink
3 Replies

9. Shell Programming and Scripting

displaying the path in the command line

Hi all, Does anyone know how to ammend the .cshrc file in $HOME for your session to display the path as part of the command line? So that I dont need to keep on typing pwd to see where I am? thanks Ocelot (2 Replies)
Discussion started by: ocelot
2 Replies

10. UNIX for Dummies Questions & Answers

Displaying what a command is doing/has done

I would like to see what a command is doing. For example, say I move 10 files using mv * somedir. Can I use some other command to see a verification of each files movement. Like: file1 moved to somedir file2 moved to somedir ... but, i'd to have this capability for all commands. it seems... (1 Reply)
Discussion started by: quantumechanix
1 Replies
Login or Register to Ask a Question