Bash Script: Read names one on each line and perform a certain task on each

Discussion in 'Programming' started by gyahoo, Nov 23, 2014.

  1. #1
    Hello,

    I'm writing a bash script, creating users.

    I want to read a text file with names line by line and perform a function on each.

    I've tried googling a lot, but nothing is working out for me.

    I want user to enter the path of file with the names in it one on each line and then I'll add function on it.
    How can I do this?
    I'd appreciate a little help on that,
    Regards
     
    gyahoo, Nov 23, 2014 IP
  2. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #2
    I don't know bash, and i don't know any good bash tutorials on this, nor how to search for that particular search actually. In your case I would use PHP (which can be called in console mode from the commandline, python too i think) to call unix useradd with perhaps a config file param, contents of which created by PHP file_put_contents()
     
    seductiveapps.com, Nov 26, 2014 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Make sure that the file is executable before running it. File name needs to be absolute or within the same directory.

    
    #!/bin/bash
    # Read filename from stdin
    echo "Enter file path:"
    read fileName
    
    # Output each line
    while read line;do
        echo $line
    done < $fileName
    
    Code (markup):
    Another way of doing it is passing the filename as a parameter instead of requiring user entry (like: ./script filename).
     
    ThePHPMaster, Nov 26, 2014 IP
    gyahoo likes this.