-1

in need to make a loop symbolic link to any file i want on any users i each ..!

i can use this command

awk -F: ' { p="/home/"$1; printf "%s\n%s\n%s\n",p"/public_html/example.php",p"/www/example.html",p"/tmp/example.txt" }' /etc/passwd | sort

but how can make a loop to all users and the output for loop like ?????????:

example.php > /home/user1/public_html/example.php
example.html > /home/user2/www/example.php
example.php > /home/user2/tmp/example.txt
example.php > /home/user3/public_html/example.php
example.php > /home/user3/www/example.html
example.php > /home/user3/tmp/example.txt
[...snip...]

And I mean that the way .. The work is repeated or test paths for the selected files above And the creation a symbolic link for all rights files in each path by using the command

ln -s

i try to Executable the commands

#/bin/bash
mkdir folder
a=awk -F: ' { p="/home/"$1; printf "%s\n%s\n%s\n",p"/public_html/example.php",p"/www/example.html",p"/tmp/example.txt" }' /etc/passwd | sort
ln -s "$a" > folder
done

But it fails

and i wait your answer ,,,thank you stackoverflow.com

saba
  • 35
  • 1
  • 10
  • I have trouble making sense of your question. 1. In your sample output you line up files with different extensions, (e.g. `.html` versus `.php` in the second line), is this intentional? – ex-bart Aug 21 '15 at 12:59
  • 2. In your sample bash script you invoke the command `-F:` with the shell variable `a` set to `awk`. You probably meant to assign the result of the command to `a` instead, right? E.g. `a=$(awk -F: ... | sort)`? – ex-bart Aug 21 '15 at 13:02
  • 3. In your sample bash script you execute `ln -s` with a single argument (the complete contents of the shell variable `a`, including all whitespace and embeded newlines). However, `ln -s` requires exactly two arguments, a source and a destination. You proceed to redirect the standard output of that command to `folder`. However, for that to work, folder must be a file, or must be creatable as a file, but you have created a folder by that name in the second line. – ex-bart Aug 21 '15 at 13:07
  • hello sir .. the extensions just an example and i want to make symbolic link to PHP extension for config.php ,, and the i want the symbolic for all users and test all paths of configs ... – saba Aug 21 '15 at 13:09
  • welcome i want to make symlink for config.php , i test all paths of configs like /home/user/public_html/wp-config.php and make symlink it .. to all users – saba Aug 21 '15 at 13:13
  • So you want to search each user's home directory for a number of files, and create a symlink names e.g. `/home/$user/folder/config.php`, and pointing to the first file you found for that user? – ex-bart Aug 21 '15 at 13:17
  • Yes, exactly that's right .. I want to create symbolic link for each user Where it is the work of a repeat of the number of configs path And after the work is a symbolic link for each existing file I collected all paths of configs www/config.php wp-config.php include/config.php wp/wp-config.php includes/conf.php ......etc...... i wait your answer and thanks so much – saba Aug 21 '15 at 13:24

1 Answers1

0

Try something like this:

IFS=:
while read user rest; do
    for file in public_html/example.php www/example.php; do
        if [ -e "/home/$user/$file" ]; then
             mkdir -p "/home/$user/folder"
             ln -s "/home/$user/$file" "/home/$user/folder/example.php"
             break
        fi
    done
done </etc/passwd

This parses /etc/passwd in the shell. IFS=: sets the internal field separator, which is than used by read to split the lines read into fields. The first field is assigned to the shell variable user, and all remaining fields are assigned to rest (and ignored).

Note: This is probably OK for a script that is used once, but there are several issues you should be aware of.

  1. You probably don't want to do the symlinking for all user, just for regular ones. This the script ensures this by assuming the user's home directory is /home/$user, which typically excludes root and other special-purpose users. However, this is fragile, e.g. the home directory can be named differently on some systems.

  2. No symlink is created if none of the files were found in the user's home directory.

  3. The permissions of the symlink are not adjusted. The resulting symlink has the permissions that the script was running with.

ex-bart
  • 1,352
  • 8
  • 9
  • hello thanks for your replay but i Execution #/bin/bash IFS=: while read user rest; do for file in public_html/wp-config ; do if [ -e "/home/$user/$file" ]; then mkdir -p "/home/$user/folder" ln -s "/home/$user/$file" "/home/$user/folder/example.php" break fi done done – saba Aug 22 '15 at 14:08