Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Scripting Question (For Loop) Post 302952985 by shah9250 on Monday 24th of August 2015 06:15:17 PM
Old 08-24-2015
Scripting Question (For Loop)

Hello,

I am practicing creating loops in bash scripting. I am trying to understand the different uses of the for loop. I have a script I made below with some help.

Code:
#/bin/bash

#This script will take the extension of a file in ls command and echo a certain statement based on what type of file it is (.sh , .txt etc)

for filename in $(ls)

	do
	ext=${filename#*.}
		case "$ext" in
		sh) echo "This a script file";;
		txt) echo "This is a text file";;
		*) echo "Never mind, it is none of these 2" ;;
		esac
	done

The objective was to take the output of the "ls" command and classify the files found as either script or text file, based on the extracted characters after the "." in the name. I am wanting to know what does "filename" mean in the for loop. I understand it takes the filenames from the output of "ls" and runs the action, but conceptually what does "filename" represent? I want to find other things I can put instead of "filename" and learn more so I could search the topic, but I don't know what it was called. Thanks!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

new to shell scripting: whats wrong with my if loop

#!/bin/bash for file in $HOME/*; do if ; then rm -i $file > /dev/null echo "$?" echo "$file has been deleted" fi done Been trying to learn shell scripting for a week or so now, when i run the script it doesnt display an error message, seems like it runs fine, however it doesnt delete... (10 Replies)
Discussion started by: stride6
10 Replies

2. Solaris

Problem in for loop of shell scripting in solaris

Hi below is my script for((i=0;i<=$TOTAL;i++)) do echo "IP's created are $s1.$s2.$s3.$s4" s4=`expr $s4 + 1` done where s1,2,3,4 are input varibles below error occurs while running the script syntax error at lin 11: '(' unexpected ... (12 Replies)
Discussion started by: krevathi1912
12 Replies

3. UNIX for Dummies Questions & Answers

Loop till you find a string in a fine <-- Need Help New to Unix Scripting

Guys - I am new to Unix scripting and am in need for a script that does the following. I have bits and pieces created and tested but i am just having a little difficult time getting it all together. - Loop through till it finds a string in a specific file. Any help is greatly appreciated. ... (1 Reply)
Discussion started by: mrehman
1 Replies

4. Shell Programming and Scripting

while loop in shell scripting

Hi, I have a doubt in usage of while loop in Shell script as Iam new to this. My requirement is that,I have two different directories containing some files which move files to other folder after parsing is done. In my script i wanted to add a while loop which checks for the count in these two... (5 Replies)
Discussion started by: jyothi_wipro
5 Replies

5. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

6. Shell Programming and Scripting

Clarification on if loop in Shell scripting

Hi, I'm using Ksh and I'm seeing some of code in my programme as given below. Could you please let me know whats is this meeaing ? (I'm new to this unix) grep "1034" /u/kkk/bin/temp5.lst|cut -c1-2 >/u/kkk/bin/temp6.lst if then echo "" ... (2 Replies)
Discussion started by: shyamu544
2 Replies

7. Shell Programming and Scripting

while loop shell scripting help

hi i was wondering if someone can help me with a while loop..i have been looking at this for hours and dont no wut to do.. i have to make a menu style.. to have a beeter understanding i have linked a photo at the bottom... http://www.mypicx.com/uploadimg/772204432_08022011_1.pngand then ... (1 Reply)
Discussion started by: beerpong1
1 Replies

8. Homework & Coursework Questions

shell scripting while loop lab 15 help

hi.. this is shell scripting lab15.sh i dont understand this lab i am at the screen shot part. i was wondering if someone can take a quick look at this lab i have linked the doc below. i dont know where to start i have did the Required Errorlevels Errorlevel # but dont... (1 Reply)
Discussion started by: beerpong1
1 Replies

9. Shell Programming and Scripting

For loop scripting

Hi there again, I need some help in figuring out how to execute a for loop command to come up with my desired output. I have individual monthly files and a single file containing a list of location data for 1 year. I want to do a loop where for each monthly file say Jan.hdf, if will read the... (8 Replies)
Discussion started by: ida1215
8 Replies

10. Shell Programming and Scripting

Expect Scripting Loop Argument Desperately Needed!

I am trying to create an Expect script that does the following: 1) Telnets to an IP address and logs in with user ID and Password 2) Issue a CLI command to the server that will output data of which I am particularly interested in a DS1 clock 'Slips' value. I want to be able to keep issuing... (0 Replies)
Discussion started by: dwightlaidler
0 Replies
Excel::Template::Container::Loop(3pm)			User Contributed Perl Documentation		     Excel::Template::Container::Loop(3pm)

NAME
Excel::Template::Container::Loop - Excel::Template::Container::Loop PURPOSE
To provide looping NODE NAME
LOOP INHERITANCE
Excel::Template::Container ATTRIBUTES
o NAME This is the name of the loop. It's used to identify within the parameter set what variables to expose to the children nodes each iteration. CHILDREN
None EFFECTS
None DEPENDENCIES
None USAGE
<loop name="LOOPY"> ... Children here ... </loop> In the above example, the children nodes would have access to the LOOPY array of hashes as parameters. Each iteration through the array would expose a different hash of parameters to the children. These loops work just like HTML::Template's loops. (I promise I'll give more info here!) There is one difference - I prefer using Perl-like scoping, so accessing of variables outside the LOOP scope from within is perfectly acceptable. You can also hide outside variables with inner values, if you desire, just like Perl. AUTHOR
Rob Kinyon (rob.kinyon@gmail.com) SEE ALSO
perl v5.14.2 2012-04-29 Excel::Template::Container::Loop(3pm)
All times are GMT -4. The time now is 03:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy