Zeilenweise Möglichkeiten
Wenn man Informationen interaktiv vom Benutzer oder zeilenweise aus Dateien benötigt, kann auf das Programm read zurückgreifen. Zusätzlich kann man über einen angegebenen Separator auf einzelne Felder in einer Zeile zurückgreifen. Dazu habe ich hier merkenswerte Mehrzeiler aufgeschrieben.
Linux Benutzer für SQL-Import vorbereiten
#!/bin/bash # exportUser2SQL.sh # Exportiert alle User aus der /etc/passwd in ein SQL-Dump-File # rm ~/insertIntoUsers.sql while IFS=':' read uid password uidNumber gidNumber gecos home shell; do echo "INSERT INTO users values ('$uid','x',$uidNumber,$gidNumber,'$gecos','$home','$shell');" >> ~/insertIntoUsers.sql; done < /etc/passwd
-- insertIntoUsers.sql INSERT INTO users VALUES ('root','x',0,0,'root','/root','/bin/bash'); INSERT INTO users VALUES ('bin','x',1,1,'bin','/bin','/sbin/nologin'); INSERT INTO users VALUES ('daemon','x',2,2,'daemon','/sbin','/sbin/nologin'); INSERT INTO users VALUES ('adm','x',3,4,'adm','/var/adm','/sbin/nologin'); INSERT INTO users VALUES ('lp','x',4,7,'lp','/var/spool/lpd','/sbin/nologin'); INSERT INTO users VALUES ('sync','x',5,0,'sync','/sbin','/bin/sync'); INSERT INTO users VALUES ('shutdown','x',6,0,'shutdown','/sbin','/sbin/shutdown'); INSERT INTO users VALUES ('halt','x',7,0,'halt','/sbin','/sbin/halt'); INSERT INTO users VALUES ('mail','x',8,12,'mail','/var/spool/mail','/sbin/nologin'); INSERT INTO users VALUES ('news','x',9,13,'news','/etc/news',''); INSERT INTO users VALUES ('uucp','x',10,14,'uucp','/var/spool/uucp','/sbin/nologin'); INSERT INTO users VALUES ('operator','x',11,0,'operator','/root','/sbin/nologin'); INSERT INTO users VALUES ('games','x',12,100,'games','/usr/games','/sbin/nologin'); INSERT INTO users VALUES ('gopher','x',13,30,'gopher','/var/gopher','/sbin/nologin'); INSERT INTO users VALUES ('ftp','x',14,50,'FTP User','/var/ftp','/sbin/nologin'); INSERT INTO users VALUES ('nobody','x',99,99,'Nobody','/','/sbin/nologin'); INSERT INTO users VALUES ('nscd','x',28,28,'NSCD Daemon','/','/sbin/nologin'); INSERT INTO users VALUES ('vcsa','x',69,69,'virtual console memory owner','/dev','/sbin/nologin'); INSERT INTO users VALUES ('pcap','x',77,77,'','/var/arpwatch','/sbin/nologin'); INSERT INTO users VALUES ('dbus','x',81,81,'System message bus','/','/sbin/nologin'); INSERT INTO users VALUES ('rpc','x',32,32,'Portmapper RPC user','/','/sbin/nologin'); INSERT INTO users VALUES ('mailnull','x',47,47,'','/var/spool/mqueue','/sbin/nologin'); INSERT INTO users VALUES ('smmsp','x',51,51,'','/var/spool/mqueue','/sbin/nologin'); INSERT INTO users VALUES ('sshd','x',74,74,'Privilege-separated SSH','/var/empty/sshd','/sbin/nologin'); INSERT INTO users VALUES ('rpcuser','x',29,29,'RPC Service User','/var/lib/nfs','/sbin/nologin'); INSERT INTO users VALUES ('nfsnobody','x',65534,65534,'Anonymous NFS User','/var/lib/nfs','/sbin/nologin'); INSERT INTO users VALUES ('haldaemon','x',68,68,'HAL daemon','/','/sbin/nologin'); INSERT INTO users VALUES ('avahi-autoipd','x',100,102,'avahi-autoipd','/var/lib/avahi-autoipd','/sbin/nologin'); INSERT INTO users VALUES ('ntp','x',38,38,'','/etc/ntp','/sbin/nologin'); INSERT INTO users VALUES ('avahi','x',70,70,'Avahi daemon','/','/sbin/nologin'); INSERT INTO users VALUES ('apache','x',48,48,'Apache','/var/www','/sbin/nologin'); INSERT INTO users VALUES ('hsqldb','x',96,96,'','/var/lib/hsqldb','/sbin/nologin'); INSERT INTO users VALUES ('xfs','x',43,43,'X Font Server','/etc/X11/fs','/sbin/nologin'); INSERT INTO users VALUES ('gdm','x',42,42,'','/var/gdm','/sbin/nologin');
Linux Gruppen für SQL-Import vorbereiten
#!/bin/bash # exportGroupMember2SQL.sh # Wer die Informationen aus den Linux-Gruppen in einer Datenbank # benötigt, kann sich diese einfach als INSERT-Statements vorbereiten: rm ~/insertIntoGroups.sql rm ~/insertIntoMembers.sql while IFS=':' read gid password gidNumber members; do echo "INSERT INTO groups values ('$gid','$password',$gidNumber);" >> ~/insertIntoGroups.sql; # Kommas durch Leerzeichen ersetzen, damit FOR-Schleife genutzt werden kann. grpmembers=$(echo $members | tr ',' ' ') for member in $grpmembers; do echo "INSERT INTO members values($gidNumber,'$member');" >> ~/insertIntoMembers.sql; done done < /etc/group
-- insertIntoGroups.sql INSERT INTO groups VALUES ('root','x',0); INSERT INTO groups VALUES ('bin','x',1); INSERT INTO groups VALUES ('daemon','x',2); INSERT INTO groups VALUES ('sys','x',3); INSERT INTO groups VALUES ('adm','x',4); INSERT INTO groups VALUES ('tty','x',5); INSERT INTO groups VALUES ('disk','x',6); INSERT INTO groups VALUES ('lp','x',7); INSERT INTO groups VALUES ('mem','x',8); INSERT INTO groups VALUES ('kmem','x',9); INSERT INTO groups VALUES ('wheel','x',10); INSERT INTO groups VALUES ('mail','x',12); INSERT INTO groups VALUES ('news','x',13); INSERT INTO groups VALUES ('uucp','x',14); INSERT INTO groups VALUES ('man','x',15); INSERT INTO groups VALUES ('games','x',20); INSERT INTO groups VALUES ('gopher','x',30); INSERT INTO groups VALUES ('dip','x',40); INSERT INTO groups VALUES ('ftp','x',50); INSERT INTO groups VALUES ('lock','x',54); INSERT INTO groups VALUES ('nobody','x',99); INSERT INTO groups VALUES ('users','x',100); INSERT INTO groups VALUES ('nscd','x',28); INSERT INTO groups VALUES ('utmp','x',22); INSERT INTO groups VALUES ('utempter','x',35); INSERT INTO groups VALUES ('floppy','x',19); INSERT INTO groups VALUES ('vcsa','x',69); INSERT INTO groups VALUES ('pcap','x',77); INSERT INTO groups VALUES ('slocate','x',21); INSERT INTO groups VALUES ('dbus','x',81); INSERT INTO groups VALUES ('audio','x',63); INSERT INTO groups VALUES ('rpc','x',32); INSERT INTO groups VALUES ('mailnull','x',47); INSERT INTO groups VALUES ('smmsp','x',51); INSERT INTO groups VALUES ('sshd','x',74); INSERT INTO groups VALUES ('ecryptfs','x',101); INSERT INTO groups VALUES ('rpcuser','x',29); INSERT INTO groups VALUES ('nfsnobody','x',65534); INSERT INTO groups VALUES ('haldaemon','x',68); INSERT INTO groups VALUES ('avahi-autoipd','x',102); INSERT INTO groups VALUES ('ntp','x',38); INSERT INTO groups VALUES ('avahi','x',70); INSERT INTO groups VALUES ('apache','x',48); INSERT INTO groups VALUES ('hsqldb','x',96); INSERT INTO groups VALUES ('xfs','x',43); INSERT INTO groups VALUES ('gdm','x',42);
-- insertIntoMembers.sql INSERT INTO members VALUES(0,'root'); INSERT INTO members VALUES(1,'root'); INSERT INTO members VALUES(1,'bin'); INSERT INTO members VALUES(1,'daemon'); INSERT INTO members VALUES(2,'root'); INSERT INTO members VALUES(2,'bin'); INSERT INTO members VALUES(2,'daemon'); INSERT INTO members VALUES(3,'root'); INSERT INTO members VALUES(3,'bin'); INSERT INTO members VALUES(3,'adm'); INSERT INTO members VALUES(4,'root'); INSERT INTO members VALUES(4,'adm'); INSERT INTO members VALUES(4,'daemon'); INSERT INTO members VALUES(6,'root'); INSERT INTO members VALUES(7,'daemon'); INSERT INTO members VALUES(7,'lp'); INSERT INTO members VALUES(10,'root'); INSERT INTO members VALUES(12,'mail'); INSERT INTO members VALUES(13,'news'); INSERT INTO members VALUES(14,'uucp'); INSERT INTO members VALUES(63,'gdm');