loops - Assistance with bash code to extract data line by line -
i've been struggling new bash script i'm attempting write.
i have text file following contents:
/home/user1/public_html/pathtofile1 /home/user2/public_html/pathtofile2 /home/user3/public_html/packtofile3 i need code read text file line line, extract username can suspend , put full path (/home/user1/public_html/pathtofile1) reason getting suspended.
example of need automatically do:
/scripts/suspendacct user1 '/home/user1/public_html/pathtofile1' 1 /scripts/suspendacct user2 '/home/user1/public_html/pathtofile2' 1 /scripts/suspendacct user3 '/home/user1/public_html/pathtofile3' 1 i've tried "for loop" , "while read line" , i'm having 0 luck.
also needs exclude duplicate usernames.
any input appreciated. in advance.
your while loop approach fine, ran trouble parsing line. can use either parameter expansion/substring extraction or substring replacement parse needed information line. below example of parameter expansion/substring extraction:
#!/bin/bash while read -r line; usr="${line#/*/}" usr="${usr%%/*}" # /scripts/suspendacct $usr "'$line'" # uncomment use printf "/scripts/suspendacct %s '%s'\n" $usr "$line" # delete use done exit 0 temporary output
$ bash suspendusr.sh <dat/usrpathtofile.txt /scripts/suspendacct user1 '/home/user1/public_html/pathtofile1' /scripts/suspendacct user2 '/home/user2/public_html/pathtofile2' /scripts/suspendacct user3 '/home/user3/public_html/packtofile3' note: have printing /scripts/suspendacct call each user. have adjust script , uncomment actual call.
as have figured out, there half-dozen ways this. fine. choose approach comfortable with.
skip duplicates
declare -a uarray declare -i found=0 while read -r line; usr="${line#/*/}" usr="${usr%%/*}" found=0 in "${uarray[@]}"; [ "$usr" = "$i" ] && found=1; done [ $found -eq 1 ] && continue uarray+=( $usr ) # /scripts/suspendacct $usr "'$line'" # uncomment use printf "/scripts/suspendacct %s '%s'\n" $usr "$line" # delete use done using individual check on each user better using name regex. prevent lesser included matches. (e.g. user11 , user111)
Comments
Post a Comment