LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-02-2015, 09:31 AM   #1
Saridonas
LQ Newbie
 
Registered: May 2015
Posts: 5

Rep: Reputation: Disabled
Question Help for basics of shell


I'm super newb and want to start learning by doing simple stuff. So maybe you could give me some support on those tasks -
Creating a procedure that would take file's name as an argument checking current folder AND IF there is no such file (under the name of argument just taken) it would print the message and create a file under mentioned name.

I bet completing this task would "get me on the track" and I could continue from there on my own for a while.

THANKS!
 
Old 05-02-2015, 09:50 AM   #2
DJ Shaji
Member
 
Registered: Dec 2004
Location: Yo Momma's house
Distribution: Fedora Rawhide, ArchLinux
Posts: 518
Blog Entries: 15

Rep: Reputation: 106Reputation: 106
Read the manual page on test and touch. Will take at most 5 minutes. What you are asking is the simplest of things.
 
1 members found this post helpful.
Old 05-02-2015, 12:16 PM   #3
Saridonas
LQ Newbie
 
Registered: May 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by DJ Shaji View Post
Read the manual page on test and touch. Will take at most 5 minutes. What you are asking is the simplest of things.
Thank you, those two commands worked perfect. I moved on a little more and now I would like to know how to check for subdirectories in argumented directory (or current directory)? Any leads on that?
 
Old 05-02-2015, 12:27 PM   #4
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Same 'test' command with '-d' switch does.

Code:
for d in *; do test -d "$d" && echo $d; done
 
1 members found this post helpful.
Old 05-02-2015, 12:47 PM   #5
Saridonas
LQ Newbie
 
Registered: May 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by veerain View Post
Same 'test' command with '-d' switch does.

Code:
for d in *; do test -d "$d" && echo $d; done
SO made script
Code:
for d in /home/paulius/Desktop/
do
test -d "$d" &&
echo $d;
done
And it outputs only
Code:
/home/paulius/Desktop/
I didn't quite understood for purpose of d (in "for d in /....") and &&.
The $d is variable, right?
 
Old 05-02-2015, 12:51 PM   #6
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
for d in /home/paulius/Desktop/
Instead put 'for d in /home/paulius/Desktop/*'.

'&&' is Binary AND operator. If previuous command succeeds then it executes the rest.

'd' is a variable and we reference it's value using '$d'.
 
1 members found this post helpful.
Old 05-02-2015, 01:00 PM   #7
Saridonas
LQ Newbie
 
Registered: May 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by veerain View Post
Instead put 'for d in /home/paulius/Desktop/*'.

'&&' is Binary AND operator. If previuous command succeeds then it executes the rest.

'd' is a variable and we reference it's value using '$d'.
That worked and printed all subcategories. Thank you.
When I tried to use $1 instead of /home/paulius/Desktop/* and then executed it
Code:
bash sh.sh /home/*
it just printed
Quote:
/home/admin
even tho there is admin and paulius categories.
 
Old 05-02-2015, 02:16 PM   #8
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: Slackware®
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Member response

Hi,

Welcome to LQ!
Quote:
Originally Posted by Saridonas View Post
I'm super newb and want to start learning by doing simple stuff. So maybe you could give me some support on those tasks -
Creating a procedure that would take file's name as an argument checking current folder AND IF there is no such file (under the name of argument just taken) it would print the message and create a file under mentioned name.

I bet completing this task would "get me on the track" and I could continue from there on my own for a while.

THANKS!
Links that I think a new user will find helpful;
Quote:
Just a few links to aid you to gaining some understanding;



1
Linux Documentation Project
2
Rute Tutorial & Exposition
3
Linux Command Guide
4
Bash Beginners Guide
5
Bash Reference Manual
6
Advanced Bash-Scripting Guide
7
Linux Newbie Admin Guide
8
LinuxSelfHelp
9
Ultimate Linux Newbie Guide
10
Linux Home Networking
11
Virtualization- Top 10

The above links and others can be found at '
Slackware-Links'. More than just Slackware® links!
Hope this helps.
Have fun & enjoy!
 
1 members found this post helpful.
Old 05-02-2015, 03:03 PM   #9
Saridonas
LQ Newbie
 
Registered: May 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
I want to make a script that creates files in selected directory.
list is the list of names input by user when executing shell file
Code:
bash shell.sh file01 file02 file03
then "for" should create three files in pre-set directory under the names of file01, file02 and file03.
Code:
list=($1 $2 $3 $4)
for i in $list
do
echo "Created file: $list"
touch /home/paulius/Desktop/shell $list
done
All this code does is creates one file and echoes Created file: file01. And that is it. What is wrong in the code?

EDIT:
Also, reading the variables from file "list.txt" would be even better. How could I do that?

EDIT2:
Code:
#!/bin/bash/

if test -s /home/paulius/Desktop/shell/shell_testas/sarasas.txt
then
for line in $(cat sarasas.txt)
do
        if test -d /home/paulius/Desktop/shell/shell_testas
        then
        echo "Created file: $line"
        touch /home/paulius/Desktop/shell/shell_testas $line
        else
        echo "No such directory"
        break
        fi
done
else
echo "Empty list"
fi
Works great for me situation.

Last edited by Saridonas; 05-02-2015 at 04:10 PM. Reason: Completed the task
 
Old 05-03-2015, 12:59 AM   #10
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
list=($1 $2 $3 $4)
change to 'list="$1 $2 $3 $4"'.

And in second code:

'touch /home/paulius/Desktop/shell/shell_testas $line'

change to:

'touch /home/paulius/Desktop/shell/shell_testas/$line'
 
Old 05-03-2015, 03:48 AM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by veerain View Post
change
Instead try to explain things.
That way the OP learns the reasons why.



Quote:
Originally Posted by Saridonas View Post
All this code does is creates one file and echoes Created file: file01. And that is it. What is wrong in the code?
What's wrong is that you seem to try to use an array but that should look like this:
Code:
ARRAY=(( $1 $2 $3 $4 ))
*Don't use arrays until you know you need them, OK?
**Also note you should not be using "for" loops idly: http://mywiki.wooledge.org/DontReadLinesWithFor
***As a matter of fact do read http://mywiki.wooledge.org/BashGuide and http://mywiki.wooledge.org/BashFAQ as it will help you tremendously.


Quote:
Originally Posted by Saridonas View Post
Code:
#!/bin/bash/
No, the hash bang line should not have a trailing slash:
Code:
#!/bin/bash

With the "for" loop gone you can start using variables to store values, "getopts" to parse input and perform error handling where its most efficient:
Code:
#!/bin/bash
# Set debug mode when testing:
set -vx
# Set default behaviour:
LANG=C; LC_ALL=C; export LANG LC_ALL

_help() { echo "scriptname: -t target directory -s source file "; exit 0; }
[ $# -eq 0 -o $# -gt 4 -o "${1:0:1}" != "-" ] && _help

while getopts s:t:h OPT; do
 case "${OPT}" in
  s) [ -s "${OPTARG}" ] || { echo "Empty file, exiting." > /dev/stderr; exit 1; }
     SOURCEFILE="${OPTARG}";;
  t) [ -d "${OPTARG}" ] || { echo "No such directory, exiting." > /dev/stderr; exit 1; }
     TARGETDIR="${OPTARG}";;
  h|*) _help;;
 esac
done

cat "${SOURCEFILE}" | while read ITEM; do
 echo -en "Creating file: \"${ITEM}\""
 touch "${TARGETDIR}/${ITEM}" || { echo "EPIC FAIL."; break; } && echo "OK."
done

exit 0
 
  


Reply



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
LXer: Shell scripting for system administrators: beyond the basics LXer Syndicated Linux News 0 10-12-2010 10:41 PM
LXer: Course: Bash Shell Basics LXer Syndicated Linux News 0 07-19-2009 06:20 PM
shell scripting basics.. how to forward double quotes ?? m_kane Linux - General 3 01-31-2009 11:45 AM
LXer: Shell, terminal, console — the basics LXer Syndicated Linux News 0 09-04-2007 06:21 PM
Shell Scripting Basics rohitsood Linux - Networking 1 06-03-2006 01:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:46 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
Open Source Consulting | Domain Registration