LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-16-2005, 10:40 AM   #1
mathias1979
LQ Newbie
 
Registered: May 2005
Location: Raleigh, NC
Posts: 7

Rep: Reputation: 0
Couple of Shell Script Questions


Two quick questions about shell, or c-shell scripting. Is there a command that will read the names of all files in a particular directory into an array so that I may loop through those files with my script without having to manually enter their names? Also, how would I go about reading just a portion of a character string into a new variable. For instance, if I had the character string "file02" stored in a variable...how could I read just the last two characters ('02') into a new variable?

-Matt
 
Old 06-16-2005, 11:41 AM   #2
JediGuy_bob
LQ Newbie
 
Registered: Apr 2004
Location: Albuquerque/Chicago
Posts: 22

Rep: Reputation: 15
You can always use a bash script using ls and cut to get the names and have perl or python take that and store it as an array.
 
Old 06-16-2005, 12:10 PM   #3
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Re: Couple of Shell Script Questions

Quote:
Originally posted by mathias1979
Two quick questions about shell, or c-shell scripting. Is there a command that will read the names of all files in a particular directory into an array so that I may loop through those files with my script without having to manually enter their names? Also, how would I go about reading just a portion of a character string into a new variable. For instance, if I had the character string "file02" stored in a variable...how could I read just the last two characters ('02') into a new variable?

-Matt
I only know bash (sh in most cases is a link to bash)

Question one:

Code:
for name in *; do ......done
be carefull and protect $name variable with " " because the filenames may contain spaces

question two:

first approach: (cutting off a part from the begging of a string that fits in a pattern)
Code:
skalkoto@darkstar:~$ var="file02"
skalkoto@darkstar:~$ var1=${var#file}
skalkoto@darkstar:~$ echo $var1
02
skalkoto@darkstar:~$
second approach: (using the string position)
Code:
skalkoto@darkstar:~$ var="file02"
skalkoto@darkstar:~$ var2=${var:4}
skalkoto@darkstar:~$ echo $var2
02
skalkoto@darkstar:~$
 
Old 06-16-2005, 04:22 PM   #4
mathias1979
LQ Newbie
 
Registered: May 2005
Location: Raleigh, NC
Posts: 7

Original Poster
Rep: Reputation: 0
Re: Re: Couple of Shell Script Questions

Quote:
Originally posted by perfect_circle

Code:
for name in *; do ......done
be carefull and protect $name variable with " " because the filenames may contain spaces

second approach: (using the string position)
Code:
skalkoto@darkstar:~$ var="file02"
skalkoto@darkstar:~$ var2=${var:4}
skalkoto@darkstar:~$ echo $var2
02
skalkoto@darkstar:~$
Both worked like charm. Thanks!

-Matt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
two shell script questions tutwabee Linux - General 4 08-10-2004 07:03 PM
Couple of questions qmon2002 Solaris / OpenSolaris 3 04-08-2004 03:30 AM
Shell Script Questions Col Panic Linux - General 13 10-12-2003 01:51 PM
A couple of questions LarcTongi Linux - General 1 03-14-2001 01:48 PM


All times are GMT -5. The time now is 08:40 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration