Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have in a file called "files" a listing of directories named - let's say - a, b, c, d, e, f etc. Each name is one line.
I would like to write a script in bash that will perform the following functions:
Code:
#!/bin/bash
ls -d -1 */|sed 's/\///' > files
for i in a c g
do
# do something with $i
done
for i in <all others>
do
# do something with any else $i
done
Example:
Do something with lines 3 and 5
Something else with line 7
Something else with the rest.
Code:
n=1
while read line; do
if [ $n -eq 3 ] || [ $n -eq 5 ]; then
echo "Do process A with line:"$n" "$line""
elif [ $n -eq 7 ]; then
echo "Do process B with line:"$n" "$line""
else
echo "Do process C with line:"$n" "$line""
fi
n=$((n + 1))
done < files
Do something with lines 3 and 5
Something else with line 7
Something else with the rest.
Its OK if You know numbers of the lines.
My problem is that I only know the subdirs names.
I have to do one thing with subdirs whose name does not change and another with other directories, which can be a different number and different names each time.
#!/bin/bash
ls -d -1 */|sed 's/\///' > files
for i in `cat files`
do
if [ $i = "a" ] || [ $i = "c" ] || [ $i = "g" ]; then
echo "Hey I found $i"; else
echo "You can ignore $i"
fi
done
$( ) and ` ` produce an argument list that is split at whitespace including embedded spaces, and also by default do "filename generation".
A while-read loop can safely read lines from a text file:
Code:
while IFS= read -r fn
do
case "$fn" in
( "a" | "c" | "g" )
echo do something with "$fn"
;;
( * )
echo do someth else with "$fn"
;;
esac
done < file
Last edited by MadeInGermany; 05-28-2023 at 12:53 PM.
how do you know a directory belongs to the first group (or the second)?
I know from this that the names of the first group of directories do not change and are called e.g. a, c, g etc.
The rest of the directories can take different names.
I know from this that the names of the first group of directories do not change and are called e.g. a, c, g etc.
The rest of the directories can take different names.
Ok, so you have some reserved names. I would make a list for them, an associative array like this:
Code:
R_DIRS[a]=1
R_DIRS[c]=1
....
for i in "${R_DIRS[@]}"
do something with the reserved names
done
for i in <all dirs>
[[ "${R_DIRS[$i]:-0}" -eq 1 ]] && continue
do something with the others
done
R_DIRS["a"]=
R_DIRS["c"]=
....
for d in "${!R_DIRS[@]}"
do
echo do something with "$d"
done
for d in <all dirs>
do
[[ ${R_DIRS[$d]+exist} ]] && continue
echo something else with "$d"
done
bash 4.2+ supports
Code:
[[ -v R_DIRS[$d] ]] && continue
Last edited by MadeInGermany; 05-30-2023 at 12:43 PM.
#!/bin/bash
shopt -s extglob nullglob
dirpat="a|c|g"
for d in @($dirpat)/
do
echo do sth with "$d"
done
for d in !($dirpat)/
do
echo do sth else with "$d"
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.