LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help for basics of shell (https://www.linuxquestions.org/questions/linux-newbie-8/help-for-basics-of-shell-4175541410/)

Saridonas 05-02-2015 09:31 AM

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!

DJ Shaji 05-02-2015 09:50 AM

Read the manual page on test and touch. Will take at most 5 minutes. What you are asking is the simplest of things.

Saridonas 05-02-2015 12:16 PM

Quote:

Originally Posted by DJ Shaji (Post 5356634)
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?

veerain 05-02-2015 12:27 PM

Same 'test' command with '-d' switch does.

Code:

for d in *; do test -d "$d" && echo $d; done

Saridonas 05-02-2015 12:47 PM

Quote:

Originally Posted by veerain (Post 5356682)
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?

veerain 05-02-2015 12:51 PM

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'.

Saridonas 05-02-2015 01:00 PM

Quote:

Originally Posted by veerain (Post 5356691)
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.

onebuck 05-02-2015 02:16 PM

Member response
 
Hi,

Welcome to LQ!
Quote:

Originally Posted by Saridonas (Post 5356627)
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!
:hattip:

Saridonas 05-02-2015 03:03 PM

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.

veerain 05-03-2015 12:59 AM

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'

unSpawn 05-03-2015 03:48 AM

Quote:

Originally Posted by veerain (Post 5356893)
change

Instead try to explain things.
That way the OP learns the reasons why.



Quote:

Originally Posted by Saridonas (Post 5356730)
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 (Post 5356730)
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



All times are GMT -5. The time now is 10:20 PM.