There seem to be a few misconceptions at work here:
First, about $(ls). This is a device called "process substitution" and it works like this: the command inside the brackets is executed, its output is taken and put on the commandline. Then the resulting commandline is executed. In your case the command is "ls" but it could be any simple or cmplicated command:
This will first execute "ls", then feed its output to "grep '^a'" (which will filter out all filenames starting with "a"). Suppose there are two files, "afile1" and "afile2". These strings will be put on the command line replacing the original command, resulting in:
and this command will finally be executed. You can use this always instead of a normal string.
Quote:
Originally Posted by shah9250
The for-loop in shell languages works quite differently than in other languages. Normally there are 4 elements to a for-loop: a variable, a starting value, an increment and an endcondition. The variable is loaded with the starting value, then the loop is executed each time incrementing the loop variable, until the endcondition is finally reached, upon which the loop is terminated:
this means: the loop variable is "i" and is loaded initially with the value 1 ("i=1"). Each iteration of the loop the value is incremented by 1 ("i++", which is an abbreviation for "i=i+1") and each iteration the end condition ("i<=10") is checked. Once i becomes bigger than 10, the loop terminates.
A shell for-loop works similar, but it is given a list of values to set the variable to:
means: in the first iteration set variable "X" to value "a", in the second to "b", and so on, until the last, "g", is reached, upon which the loop terminates.
Combine this with the above and you see:
"filename" is the name of the variable to be used and the list of values is the output of "ls", which produces a list of filenames.
Finally:
Quote:
Originally Posted by shah9250
#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)
The "type" of a file is - unlike in DOS or Windows - not determined by its extension. In Windows every file with the extension ".EXE" is executable, everything else is not. In Unix there are the rights (issue "ls -l" to see them on the leftmost column), which determine if a file is executable or not. If the "x"-flag is set the OS will try to execute it, if not, then not.
It is true that most name files conventionally according to their respective types: ".sh" for shell scripts, ".txt" for text files and so on. But this is just convention! There is no hard rule to do so and if you name a shell script "script.txt" it will still get executed if you have set the execute-flag (via the "chmod"-command). This may not be a god idea, because it will be confusing, but technically there is nothing that keeps you from doing it.
The "type" of a file is a different concept in UNIX and it is controlled via the file "/etc/magic". This file contians rule definitions what constitutes a certain file type based on the respective files contents. For instance, there could be a rule that a file starting with "#! /bin/sh" in the first line is a shell script. execute
and the file command will figure out the file type based on consulting the rules in "/etc/magic". You can even extend the ruleset with your own rules and file types.
#!/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)
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)
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)
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)
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)
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)
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)
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)
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)
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)