LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2018, 04:25 PM   #1
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Rep: Reputation: 13
Novice. bash for i in $1 construct


Linux novice
In a bash script is found:

Code:
getInputFiles () {
for i in $1
do
   if [ ! -e "$i" ]
   then
     continue
   fi
...
done
All calls to this function use command line arguments for the function arguments, but nothing says what those arguments should be.
I see the -e and $1 meaning: If it is not true that the file named $1 exists then ...
But then, that for i in a file name does not make sense to me.
Do the double quotes around the $1 make things different?
My searches have not been fruitful.
Where can I find a web page that describes this.

Edit: I have extracted the function into a test script trying to get the do loop to cycle. I used an directory name (containing several files), a file, and a quoted pair of files to no avail.

Edit 2:
I really should have put the whole thing in here. But I cannot copy and paste and a bit of dyslexia makes re-typing a bit of a problem. But here it is and please try to be tolerant of typos.

Code:
getInputFiles () {
   sortfiles=""
   echo "args are $@"
   for i in $1
   do
      if [ ! -e "$i" ]
      then
         echo "first if taken"
         continue
      fi

      echo "finished section 1"
      if [ ! -r "$i" ]
      then
         echo 1>&2 "$i file is not readable"
         exit 1
      fi

      echo "finished section 1"
      if [ ! -s "$i" ]
      then
         echo 1>&2 "$i is empty"
         exit 1
      fi

   sortfiles="$sortfiles $2 \"$i\""
  done
  echo $sortfiles
  echo "exiting function"
}
EDIT 3: Added diagnostic echos.

Last edited by bkelly; 08-31-2018 at 09:58 AM. Reason: more info
 
Old 08-30-2018, 05:01 PM   #2
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
It is just a guess.

But :
Code:
if [ ! -e "$1" ]
Should be :
Code:
if [ ! -e "$i" ]
And so, you call it :
Code:
getInputFiles "blah bloh bluh"
It will not do what is in '...' if blah, bloh and bluh files does not exist.

Otherwise I'm skeptical as you are.
 
Old 08-30-2018, 05:34 PM   #3
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
In post 2 you are correct, that was a typo on my part. I have corrected and checking my test file.
 
Old 08-31-2018, 06:19 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Maybe it is meant to be called like this:

Code:
./myscript "input1.txt input2.txt input3.txt" "output.txt"
or:
Code:
./myscript "$(echo input*.txt)" "output.txt"
 
Old 08-31-2018, 06:30 AM   #5
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
Quote:
Originally Posted by NevemTeve View Post
Maybe it is meant to be called like this:

Code:
./myscript "input1.txt input2.txt input3.txt" "output.txt"
or:
Code:
./myscript "$(echo input*.txt)" "output.txt"
I think so.

The purpose is to get a list of input files that exist, are readable and not empty.

The first argument is the list of file to check.
The second seems to be a current list of files which have already been checked.
 
Old 08-31-2018, 09:54 AM   #6
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
I request primary focus on these two points first please.
How should I read that for statement?. The behavior appears to expect a list of files.
And what is the purpose of the 1>&2 in the statement
echo 1>&2 "$i some text"

And now for additional complexity.

The function is called later in the script and looks like this:
Code:
ftmatspresortlist=`getInputFiles "$TMATS_PRE_SORTED" ""`
The single quotes are from the top left key to the left the number one key, not the standard that is shared with double quotes. When that line is added to the script to call the function then most of the echos in the function are suppressed. Including the first echo before the for loop. (see the edits in the OP that added a few echos.) When the calling function is changed to:
Code:
getInputFile "$TMATS_PRE_SORTED"
All the echoes work again.
I am confused.

Edit 1:
Even more confused. When using the more simple function call, and just putting a variable to capture the return value, that causes all the echos in the function to no longer echo to the screen. That seems reasonable. Maybe it will echo to the return value.
Except
The variable to catch the return value is unchanged.

Code:
ftmatspresortlist="default"
ftmatspresortlist=getInputFiles "$TMATS_PRE_SORTED"
echo "ftmatspresortlist = $ftmatspresortlist"
and "default" is output.

Please bear in mind that I am a Linux and Bash novice.

Edit
there is something funky that might be related. I have posted that question here:
https://www.linuxquestions.org/quest...15#post5898415

Last edited by bkelly; 08-31-2018 at 10:53 AM.
 
Old 08-31-2018, 11:14 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Q: What does echo "text" >&2 or echo "text" 1>&2 do?
A: It writes the messages to the standard error.

Q: How to get the output of a command into a variable?
A: Use the $() syntax:
Code:
Var1="$(cat somefile)"
Var2="$(echo *)"
Var3="$(getInputFiles "$TMATS_PRE_SORTED")"
Bonus:
Q: How can I test the problematic for statement in my terminal? I don't have such $1 when using a shell in terminal.
A: You can assign values to $1 $2 etc with set command:
Code:
set 'some words in the first parameter' 'other words in the second'
for i in $1; do echo "$i"; done
some
words
in
the
first
parameter

Last edited by NevemTeve; 08-31-2018 at 12:05 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem with Bash Script, supposed to construct certain sized files of grep results. FenrirXFenris Programming 9 01-30-2014 11:12 AM
[SOLVED] Nested while loops for bash novice E-Rich6 Programming 4 11-30-2012 10:53 AM
[SOLVED] a simple bash construct to generate a list nass Slackware 18 02-21-2011 07:04 PM
Novice needs Bash help. manwithaplan Programming 12 02-25-2009 08:29 PM
how to construct a compiler dayalan_cse Programming 10 08-31-2007 05:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration