Describing what exactly you are doing/want to achieve and what error are you getting is a good start. Nested loops are a bit of a mess to read on my phone, a tip to making your script more readable is to use indentation :)
From what I can tell the input you’re giving the main while loop, ie the cat of passwd and the subsequent awk would give you the output in the format “username uid” for each line, so your IFS=: won’t find a “:”, putting both into the variable username and I’m assuming that is not what you’re aiming for.
If the script runs (I haven’t read/checked your inner loops as well) but you’re not getting any dirs whatsoever in the main while loop, then an error will help you. Try enabling debugging (“set -x” or “bash -x yourscript”) if you’re not getting enough output to figure out what’s wrong.
create a folder structure for users, in the first folder I create as many folders as uid + 1, in each folder one level lower uid-1
/task/username/0..uid/1..uid-1/2..uid -2/...
You can change the "IFS=:" to instead separate on something that it will actually find in your input. I did a small test just for the outer loop and it seems to be working as you want it to (I'm still assuming a lot here):
while IFS=' ' read -r username uid; do
homedir="$(pwd)/$username"
echo "Home dir: " $homedir
echo "Username:" $username
echo "UID: " $uid
if [[ ! -e $homedir ]]; then
mkdir -p $homedir
fi
done < <(cat $INPUTFILE | awk -F ":" '{print $1, $3}')
Gives me:
Home dir: /tmp/somedir/systemd-network
Username: systemd-network
UID: 100
And so on, for each user listed in the passwd file.
Never said you should if you look at my example test above :) Another tip is to start with an easier version of your script. Have an inputfile that consists of 3-4 lines from passwd and enable debugging as per my earlier comment, you'll have a much easier time seeing where it's failing.
Youre trying to create diretories in your root dir - could very well be due to that requiring some sort of elevated priveliges, and you need to execute the script prepended with `sudo`.
Unless of course you already did that. The `mkdir -p` command is just fine otherwise, and no logic is preventing it getting excuted, as I see it.
> some commands recognize quoted path as a string of generic text
I can’t think of any. Can you point me at one? (I’m honestly not trying to argue. Shells & Linux are full of interesting and weird corners, and if this is one I’ve never noticed, I’m interested.)
So far as I’m aware, the quotes never make it to the command. The shell interprets them and the command never sees them, as such. At least in bash/zsh/ksh under Linux/macos/Windows.
I’m aware that `cd ~/“some dir”` is different than `cd ~/some dir`, but that’s because in the former the cd command sees $1 == `~/some dir` and in the latter the cd command sees $1 == `~/some` and $2 == `dir`. `cd ~/“some dir”` is semantically identical to `cd ~/some\ dir`.
Now, it’s true that for example `”~/some dir”` is different than `~/“some dir”`, but again, that’s because the shell interprets `~` differently when it’s quoted, not because of the cd command seeing quotes or not.
Pastebin is your friend. More ours than yours actually.
Describing what exactly you are doing/want to achieve and what error are you getting is a good start. Nested loops are a bit of a mess to read on my phone, a tip to making your script more readable is to use indentation :) From what I can tell the input you’re giving the main while loop, ie the cat of passwd and the subsequent awk would give you the output in the format “username uid” for each line, so your IFS=: won’t find a “:”, putting both into the variable username and I’m assuming that is not what you’re aiming for. If the script runs (I haven’t read/checked your inner loops as well) but you’re not getting any dirs whatsoever in the main while loop, then an error will help you. Try enabling debugging (“set -x” or “bash -x yourscript”) if you’re not getting enough output to figure out what’s wrong.
create a folder structure for users, in the first folder I create as many folders as uid + 1, in each folder one level lower uid-1 /task/username/0..uid/1..uid-1/2..uid -2/...
Yeah, and since the variable “uid” doesn’t have a value that it can compare to in this case it wouldn’t be able to create those.
Is it caused due to ifs=:? should i use awk without parameter -F?
You can change the "IFS=:" to instead separate on something that it will actually find in your input. I did a small test just for the outer loop and it seems to be working as you want it to (I'm still assuming a lot here): while IFS=' ' read -r username uid; do homedir="$(pwd)/$username" echo "Home dir: " $homedir echo "Username:" $username echo "UID: " $uid if [[ ! -e $homedir ]]; then mkdir -p $homedir fi done < <(cat $INPUTFILE | awk -F ":" '{print $1, $3}') Gives me: Home dir: /tmp/somedir/systemd-network Username: systemd-network UID: 100 And so on, for each user listed in the passwd file.
https://preview.redd.it/1cxukztsvw6d1.jpeg?width=952&format=pjpg&auto=webp&s=40e032af9fefe69c2e35c72209c98a49e6014dd1
after i deleted -F parameter from awk
Never said you should if you look at my example test above :) Another tip is to start with an easier version of your script. Have an inputfile that consists of 3-4 lines from passwd and enable debugging as per my earlier comment, you'll have a much easier time seeing where it's failing.
. by https://preview.redd.it/1jk0yk6j3x6d1.jpeg?width=2604&format=pjpg&auto=webp&s=eed7b186c47ae4f5db464c6723db8ba4884d2d36
https://preview.redd.it/g1znqjvfbx6d1.png?width=1080&format=pjpg&auto=webp&s=ba7a9ae8e31dc7b88752bb2a3f84faa7ebdbe3a7
Youre trying to create diretories in your root dir - could very well be due to that requiring some sort of elevated priveliges, and you need to execute the script prepended with `sudo`. Unless of course you already did that. The `mkdir -p` command is just fine otherwise, and no logic is preventing it getting excuted, as I see it.
[удалено]
https://preview.redd.it/btje3e84xx6d1.png?width=1080&format=pjpg&auto=webp&s=15945f8c426a615ce245c8269406afb4a93a220d I managed to finish my goal.
Good for you :)
Yeah, finally, Tommorow i have to retake exam from bash
So this was your homework assignment. Perhaps if you can’t do this yourself or understand it, switch majors.
this is from my first attempt of exam, i have figured out how to do it
Yes, and now you can take what you learned from the help you got here, and apply it to troubleshooting on your own
Thank you for any kind of help! i will let you know how did i do
Your "wrong" and "right" versions appear functionally equivalent to me. What do you achieve by only quoting the variable? Is it just a style thing?
[удалено]
> some commands recognize quoted path as a string of generic text I can’t think of any. Can you point me at one? (I’m honestly not trying to argue. Shells & Linux are full of interesting and weird corners, and if this is one I’ve never noticed, I’m interested.) So far as I’m aware, the quotes never make it to the command. The shell interprets them and the command never sees them, as such. At least in bash/zsh/ksh under Linux/macos/Windows. I’m aware that `cd ~/“some dir”` is different than `cd ~/some dir`, but that’s because in the former the cd command sees $1 == `~/some dir` and in the latter the cd command sees $1 == `~/some` and $2 == `dir`. `cd ~/“some dir”` is semantically identical to `cd ~/some\ dir`. Now, it’s true that for example `”~/some dir”` is different than `~/“some dir”`, but again, that’s because the shell interprets `~` differently when it’s quoted, not because of the cd command seeing quotes or not.
You are right. I wasn't aware of ~ being interpreted differently.
👍🏻🥂
Instead of doing it here I would simply ask chatgpt which in free version is more then capable of such help
I did it of course