LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2019, 03:42 AM   #1
xray55
LQ Newbie
 
Registered: Aug 2019
Posts: 19

Rep: Reputation: Disabled
Making a small batch script


Hi guys,

I want to make a small linux script to run these commands. When running the script


I want the script to ask me; ENTER USERNAME
-- That user will already be a unix user and will already have a /home directory so all I want the script to do is input what I type as username into these commands

The script then runs these 3 commands with that username


sudo mkdir /home/THATUSERNAME/main/foldera
sudo mkdir /home/THATUSERNAME/main/folderb
sudo chown -R THATUSERNAME:THATUSERNAME /home/THATUSERNAME/main

Its a repetitive thing I have to run daily.

Any help is much appreciated..

Last edited by xray55; 08-26-2019 at 03:49 AM.
 
Old 08-26-2019, 04:06 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,994

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
easiest way is something like this

mkhomedir.sh
Code:
#!/bin/bash
for dir in foldera/folderb/folderb1 foldera/folderb/folderb2
    sudo mkdir  /home/${1}/${dir}
    #sudo chown -R ${1}:${1} /home/${1}
    # edit oops, bad copypasta.. should not be in the for loop
sudo chown -R ${1}:${1} /home/${1}
done
Code:
./mkhomedir.sh newuser
it looks like you are simply creating and populating a user home dir
This isn't the way to achive that, so I will not add checks ( you would want to check if the given user exists )

check
Code:
man useradd
Code:
sudo useradd newuser -b /home -m -k -s /bin/bash
/etc/skel is a folder containing the default files and dir structure
you may want to create a new skel dir and point to that

have a peak inside `ls -la /etc/skel` to see what you already have


hope that makes sense and leads you to the correct why to create new users

Last edited by Firerat; 08-26-2019 at 04:10 AM.
 
Old 08-26-2019, 04:15 AM   #3
xray55
LQ Newbie
 
Registered: Aug 2019
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
easiest way is something like this


Hi Firerat,

Thank you for your time. Let me explain again. The users already exist and already have a home/user/main directory(s). All I want to do is create 2 directories in that main folder called foldera, folderb, and ensure they have recursive ownership. So I want the script to simply input that username it asks me into these exact commands.

So the script asks me what username? and whatever I type it, it runs these commands? is it possible?


sudo mkdir /home/THATUSERNAME/main/foldera
sudo mkdir /home/THATUSERNAME/main/folderb
sudo chown -R THATUSERNAME:THATUSERNAME /home/THATUSERNAME/main

Last edited by xray55; 08-26-2019 at 04:31 AM.
 
Old 08-26-2019, 05:02 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,930

Rep: Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192
Quote:
Originally Posted by xray55 View Post
Hi guys,

I want to make a small linux script to run these commands. When running the script


I want the script to ask me; ENTER USERNAME
-- That user will already be a unix user and will already have a /home directory so all I want the script to do is input what I type as username into these commands

The script then runs these 3 commands with that username


sudo mkdir /home/THATUSERNAME/main/foldera
sudo mkdir /home/THATUSERNAME/main/folderb
sudo chown -R THATUSERNAME:THATUSERNAME /home/THATUSERNAME/main

Its a repetitive thing I have to run daily.

Any help is much appreciated..
We're not here to do your homework assignment for you, what have you written so far?
 
Old 08-26-2019, 05:04 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
A script is just collecting together the parts you wrote at the command line, which you already seem to have a handle on.

Assuming you are using bash, you can simply google for assigning a variable and using in a script
 
Old 08-26-2019, 05:17 AM   #6
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
Congratulations on getting into scripting and automation. It will soon start to save you a lot of time and effort.

What you describe is possible. Learning to navigate the monstrously large manual page for bash (or another shell) is an important step in getting started.

Code:
man bash
As you can see it is overwhelming. However, you can search forwards and backwards with / and ? respectively, and simple patterns can be searched for also. Try to get an idea for the section headings and then it will be easier to search as a reference document. For tutorials and guides, look around the net. Greg's Bash Guide is a good place to start.

About your script:

Code:
#!/bin/sh

set -e
set -v
set -x

read -p 'Which user? ' thatusername
test -d /home/$thatusername || exit 1
sudo mkdir -p /home/$thatusername/main/foldera
sudo mkdir -p /home/$thatusername/main/folderb
sudo chown -R $thatusername:$thatusername /home/$thatusername/main

exit 0
So of those, read is built-in command. So you'll find it in the shell's manual page under the section 'Builtins' or 'SHELL BUILTIN COMMANDS', depending on whether you typed 'man sh' or 'man bash'. Then test is either a built-in in or a program, depending on the context. Above, it is a built-in so see the same manual page again.

mkdir and chown have their own manual pages.

The shell is a full scripting language, and so I recommend approaching it like one.
 
Old 08-26-2019, 05:18 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,994

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
well the script I wrote would do that , obvoiusly you would need to change since the OP edit

this is a better script
Code:
user="$1"
base_dir="/home/${user}/main"

for dir in foldera folderb
    sudo -u $user -g $user mkdir -p ${base_dir}/${dir}
done
it still seems like a waste of time
why do they need these dir ?
would the need have been expected before the user account was created?
can whatever uses them not create them if they are missing?
can the users not create them?

Last edited by Firerat; 08-26-2019 at 05:21 AM. Reason: lol, I missed ;do and in the first one also the scripts won't work but easy fix
 
Old 08-26-2019, 05:20 AM   #8
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
Quote:
Originally Posted by Firerat View Post
would the need have been expected before the user account was created?
Another option might be to modify /etc/skel so that the directories exist there and are added to all new user accounts.
 
Old 08-26-2019, 05:32 AM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,994

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by Turbocapitalist View Post
Another option might be to modify /etc/skel so that the directories exist there and are added to all new user accounts.
yeap, which is what I was hinting at in my first post

a special new one
/etc/skel_for_the_folder_ppl

I worry
A program/script that can not make these missing folders is probably not very good.
 
Old 08-26-2019, 07:57 AM   #10
xray55
LQ Newbie
 
Registered: Aug 2019
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
We're not here to do your homework assignment for you, what have you written so far?
WE? Like you speak for everybody? The core commands, I already outlined. I needed help understanding how to automate the script as my field is not script writing. That's what this forum is for and I didn't ask YOU specifically for anything.. Yes, I could have googled around but whats the point of that when you can engage and connect with others in this very helpful forum.
 
Old 08-26-2019, 07:59 AM   #11
xray55
LQ Newbie
 
Registered: Aug 2019
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
A script is just collecting together the parts you wrote at the command line, which you already seem to have a handle on.

Assuming you are using bash, you can simply google for assigning a variable and using in a script

Quote:
Originally Posted by Turbocapitalist View Post
Congratulations on getting into scripting and automation. It will soon start to save you a lot of time and effort.

What you describe is possible. Learning to navigate the monstrously large manual page for bash (or another shell) is an important step in getting started.

Code:
man bash
As you can see it is overwhelming. However, you can search forwards and backwards with / and ? respectively, and simple patterns can be searched for also. Try to get an idea for the section headings and then it will be easier to search as a reference document. For tutorials and guides, look around the net. Greg's Bash Guide is a good place to start.

About your script:

Code:
#!/bin/sh

set -e
set -v
set -x

read -p 'Which user? ' thatusername
test -d /home/$thatusername || exit 1
sudo mkdir -p /home/$thatusername/main/foldera
sudo mkdir -p /home/$thatusername/main/folderb
sudo chown -R $thatusername:$thatusername /home/$thatusername/main

exit 0
So of those, read is built-in command. So you'll find it in the shell's manual page under the section 'Builtins' or 'SHELL BUILTIN COMMANDS', depending on whether you typed 'man sh' or 'man bash'. Then test is either a built-in in or a program, depending on the context. Above, it is a built-in so see the same manual page again.

mkdir and chown have their own manual pages.

The shell is a full scripting language, and so I recommend approaching it like one.
Quote:
Originally Posted by Firerat View Post
well the script I wrote would do that , obvoiusly you would need to change since the OP edit

this is a better script
Code:
user="$1"
base_dir="/home/${user}/main"

for dir in foldera folderb
    sudo -u $user -g $user mkdir -p ${base_dir}/${dir}
done
it still seems like a waste of time
why do they need these dir ?
would the need have been expected before the user account was created?
can whatever uses them not create them if they are missing?
can the users not create them?


Thank you very much guys, you're brilliant
 
Old 08-26-2019, 08:05 AM   #12
xray55
LQ Newbie
 
Registered: Aug 2019
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Another option might be to modify /etc/skel so that the directories exist there and are added to all new user accounts.
hey Turbo. A 3rd party software creates and manages the users. I add scan paths (those folders) for another software. I guess I could learn from their community how it creates these from the start. But these scripts you guys created are wonderful.


Quote:
Originally Posted by Firerat View Post
well the script I wrote would do that , obvoiusly you would need to change since the OP edit

this is a better script
Code:
user="$1"
base_dir="/home/${user}/main"

for dir in foldera folderb
    sudo -u $user -g $user mkdir -p ${base_dir}/${dir}
done
it still seems like a waste of time
why do they need these dir ?
would the need have been expected before the user account was created?
can whatever uses them not create them if they are missing?
can the users not create them?

Hey Firerat,

They're actually scan paths for media server. The user may spell them incorrectly. Some files are private and some are shared so I can't scan the entire /home. Those directories created are where they drop shared files. I will definetly learn more about /etc/skel. Im just afraid to break things

Last edited by xray55; 08-26-2019 at 08:37 AM.
 
Old 08-26-2019, 08:54 AM   #13
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,994

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by xray55 View Post
Hey Firerat,

They're actually scan paths for media server. The user may spell them incorrectly. Some files are private and some are shared so I can't scan the entire /home. Those directories created are where they drop shared files.
ok, I better understand now
btw hope you figured out where my scripts were broken
Code:
for dir in foldera folderb;do
--------------------------^^^

ok, this is another solution
assuming everyone has the fileshare option include the dir structure in /etc/skel
also include this in in /etc/skel/.bashrc
Code:
for dir in ~/main/folder{a,b};do
     test -d ${dir} || mkdir -p ${dir}
done
each time the user starts bash it will check for those dir. and create them if they are missing

now, we can get a little smarter

we could setup groups, like public_share private_share
add users to those groups if they are allowed to share.
do away with the dir structure in /etc/skel and have
the .bashrc check if they are a member of the group before checking/creating those dirs.

Code:
#for ST in public private;do
#   groups|grep -q ${ST}_share \
#     && test -d ~/main/${ST}_share \
#          || mkdir -p ~/main/${ST}_share
#done
# bad script, if the groups test failed it would
# have created the dir anyway
# 
for ST in public private;do
   groups|grep -q ${ST}_share \
     && test ! -d ~/main/${ST}_share \
          && mkdir -p ~/main/${ST}_share
done

## much safer way
for ST in public private;do
   if [[ $(groups|grep -q ${ST}_share) ]];then
        if [[ -d ~/main/${ST}_share ]];then
            mkdir -p ~/main/${ST}_share
        fi
   fi
done
it is still crude, but better

you could futureproof
in your /etc/skel/.bashrc
Code:
. /etc/some-shares-config/mksharedirs.rc
the mksharedirs.rc contains our script
This way you could break everyones sharefolders by editing mksharedirs.rc without needing to edit every users .bashrc
when I said break I meant tweak

may seem OTT, but who knows what you will need to do in the future

Last edited by Firerat; 08-26-2019 at 09:01 AM. Reason: BAD BAD SCRIPT
 
Old 08-26-2019, 09:01 AM   #14
xray55
LQ Newbie
 
Registered: Aug 2019
Posts: 19

Original Poster
Rep: Reputation: Disabled
firerat, stop showing off, I'm jealous!
 
Old 08-26-2019, 09:10 AM   #15
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,994

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by xray55 View Post
firerat, stop showing off, I'm jealous!
did you not spot my massive error

I have a nasty habit with the compact if statements
nice to use for simple things, but prone to error as per my example above

better to use longhand if test;then do stuff;more stuff;else;other stuff;fi

in my head I did put the dir test and the mkdir into a subshell
Code:
for ST in public private;do
    groups|grep -q ${ST}_share && (
        test -d ~/main/${ST}_share \
          || mkdir -p ~/main/${ST}_share
    )
done
which would have been fine

good leason tho
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] little help on making batch sfv files steve51184 Linux - Desktop 7 06-10-2016 04:37 AM
[SOLVED] how to write a batch file in linux?and what are batch(.bat) file benefits? karthilin Linux - Newbie 8 10-15-2012 02:10 PM
Making a batch shell script to convert, merge and append (join) movie's to mkv mnt_schred Programming 8 12-07-2009 11:51 AM
Making a batch/cron for tar command ? centosfan Linux - Server 2 08-19-2008 04:29 PM
Change batch script to shell script alan.belizario Linux - Software 1 03-30-2005 01:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration