For in loop - two input variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For in loop - two input variables
# 1  
Old 09-28-2018
For in loop - two input variables

When I create a newfile, I am using the filename as a variable to create the new filename. When I ouput it, the filename contains the file extension in the middle of the file
example:
router1.txtshcdpneighbors.txt
router2.logshcdpneighbors.txt
My initial approach was to strip it out, now I would like to strip out .txt or .log. How can I add an or statement to the for in statement?

Here is the code that I have now:

Code:
for i in ./*.txts* or ./*.log*
do mv -- "$i" "${i//.txts/-s}"
done

# 2  
Old 09-28-2018
First off: generally, BUT ESPECIALLY IN SHELLSCRIPTING it helps if you tell us which shell you use and ideally which system you use.

Quote:
Originally Posted by dis0wned
Here is the code that I have now:

Code:
for i in ./*.txts* or ./*.log*
do mv -- "$i" "${i//.txts/-s}"
done

There are several things wrong with this:

Code:
for i in ./*.txts* or ./*.log*

A for-loop doesn't work in shell like in other languages: it is given a list of distinct values and the loop-variable takes one after the other value in each pass. In light of this: ./*.txts* will be expanded by the shell prior to executing the loop to a list of filenames fitting the pattern. In this case every file in the currecnt directory which name contains the string ".txts". Then it will add "or" to the list. At last it will expand ./*.log*. So finally, after the expansion, you have a statment like:

Code:
for i in ./foo.txtsx ./bar.txtsy or ./foo.logx ./whatever.logbla

and these values will be assigned consecutively to "$i". This is presumably not what you want, yes? What you perhaps want is a list of files ending either in ".txts" or in ".log". This would look like this (notice that i took the trailing asterisk out, so now it will match "file.log" but not "file.log.old"):

Code:
for i in ./*.txts ./*.log

The next issue is this command:
Code:
mv -- "$i" "${i//.txts/-s}"

The expansion ${i//.txts/-s} says: take the content of "$i", replace every occurrence of ".txts" and replace it with "-s", then display the result. So the above-mentioned example "./foo.txtsx" will become "./foo-sx" and the resulting command would be:

Code:
mv -- "./foo.txtsx" "./foo-sx"

Furthermore the files named "*.log*" will probably be unchanged at all because they probably will not contain the string ".txts" in their names. If they do a possible result would look like:

Code:
mv -- "./foo.txtsx.logbar" "./foo-sx.logbar"

Which, i suppose, is also not what you want.

One last point, but this is not a show-stopper: i suggest you format your for-loops differently so that they are easier to read:

Code:
for var in <LIST> ; do
     command "$var"
done

This way the command stands out and it is easy to see where the loop starts and ends.

Baseline is: tell us in clear-text what you want to do and we can probably better correct your code.

I hope this helps.

bakunin
# 3  
Old 09-28-2018
@bakunin, the var// / modifier exists in bash or zsh or ksh93 (and works the same).

But a "wanted output" would clarify things.
I guess you want to delete embedded strings .txt OR .log from the file names.
Unfortunately there is no OR in the variable modifier.
A solution is: one loop for each string. You can consolidate it into a function
Code:
cut_from_fnames(){
  text=$1
  subst=$2
  [ -n "$text" ] || exit
  for i in ./*"$text"*.?*
    do mv "$i" "${i/$text/$subst}"
  done
}

And run the function two times
Code:
cut_from_fnames ".log" "-"
cut_from_fnames ".txt" "-"

Notes:
In general the mv -- is a good idea if $i starts with a -
but in this case not needed because it starts with ./
The (trailing) ? glob requires one (more) character
The var// / variable modifier is greedy, while the var/ / modifier stops at the first match
This User 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. Shell Programming and Scripting

HELP - loop a curl command with different variables from input file

Hi guys! Kind of new to bash scripting and now I'm stuck. I need to curl with these variables: "{ \"nodename\": \"$1\", \"ipaddress\": \"$2\", \"poolname\": \"$3\", \"port\": \"$4\", \"loadbalancer\" : \"$5\" }" and my input_file.txt contains server001 10.10.10.01 serverpool1 80... (4 Replies)
Discussion started by: yort
4 Replies

2. Shell Programming and Scripting

Error reading two input variables

Hello all, I've been out of programming for awhile so sorry about the stupid, elementary question. I'm trying to read two inputs and compare them to a list entered as a parameter via the terminal. The script is #!/bin/bash read -p "Enter the numbers" NUM1 NUM2 for VALUE in $@; do ... (6 Replies)
Discussion started by: EnduranceMan
6 Replies

3. Shell Programming and Scripting

Passing variables to an input file

Hi All, I have to insert 2 values to a text file in specific places. I have been able to extract each variable value via a script but am not able to send these variable values to the text file. Pasted is the script for extracting the variable values: for i in `ls -1` ... (2 Replies)
Discussion started by: danish0909
2 Replies

4. Shell Programming and Scripting

To take two variables from a file and act as an input for the script

Hi, I have done the scripting such that it will read input line by line from a txt file and is passed through a script, but now my requirement is to pass two variables into a script from a file, how could I do this or is there any other better idea ? for reading singe input from a file, line... (4 Replies)
Discussion started by: ajothi
4 Replies

5. Shell Programming and Scripting

saving all input name and store them as variables

Hi I want to write a script such that when executed, it will store all input as different variable, for eg ./store.sh name1 name2 name3 name4 will result in $1=name1 $2=name2 $3=name3 etc How do I do that? Thanks. (1 Reply)
Discussion started by: piynik
1 Replies

6. Shell Programming and Scripting

How to automatically create variables from user input in ksh?

I need some help to write a ksh script. My code so far (pretty bad, sorry): #! /bin/ksh echo "Calculate average" UserDecision=y while test $UserDecision = y do echo "Enter a number: " read Number1 echo "Enter a number: " read Number2 echo "Do you want to enter another number?... (2 Replies)
Discussion started by: johnagar
2 Replies

7. Shell Programming and Scripting

For loop using input file doesn't expand variables

Hi, I'm using a for loop reading from an input file that contains files, whose path includes a variable name. But the for loop doesn't expand the variable and therefore can't find the file. Here's an example: File BACKUPFILES /home/John/alpha /home/Sue/beta... (8 Replies)
Discussion started by: Hesiod
8 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. UNIX for Advanced & Expert Users

Expanding Variables in User Input

If have var='$variable' how can I expand $variable. I have tried many thing like duble quotes/braces etc, but nothing worked. I need the solution ASAP. (2 Replies)
Discussion started by: Bsk
2 Replies
Login or Register to Ask a Question