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?
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:
There are several things wrong with this:
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:
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"):
The next issue is this command:
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:
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:
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:
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.
@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
And run the function two times
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)