If statements in Linux terminal

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions If statements in Linux terminal
# 1  
Old 10-06-2013
Question If statements in Linux terminal

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
Hello all so i just started learning linux and i need to make script which compares 2 files by size and shows on the screen the bigger one but i can't figure out how to make if statement work.






3. The attempts at a solution (include all code and scripts):
Code:
#!/bin/bash
echo "type 1st file"
read a
echo "type 2nd file"
read b
if [ stat -c %s $a > stat -c %s $b]
then
echo "$a"
else
echo "$b"
fin


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Lithuania , Vilnius , Vilniaus Collegue 1st course M.Liogys
HTML Code:
http://eif.viko.lt/lt/page/Programu-sistemos
# 2  
Old 10-06-2013
Two tips:
  • assign stat command output to two variables using syntax: size=`stat ...`
  • to compare those variables use -gt operator
Also take a look at: http://en.tldp.org/LDP/abs/html/refcards.html#AEN22313
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 10-06-2013
Code:
if [ `stat -c %s $a` -gt `stat -c %s $b` ]
then
    echo "$a"
else
    echo "$b"
fi

The more modern syntax would be something like this:
Code:
if (( $(stat -c %s file1) >  $(stat -c %s file2) ))
then
    echo "$a"
else
    echo "$b"
fi

This User Gave Thanks to fpmurphy For This Post:
# 4  
Old 10-06-2013
Thanks to you both but can u explain me what
Code:
` `

exactly does ? Because im confused.
# 5  
Old 10-06-2013
` ... ` or $( ... ) denote command substitution - the specified command is executed, and the resulting output used in place of the backquoted string.

In the above cases, stat is run, and the output stored in the variable size (in bartus' example) or used directly in a comparison (in fpmurphy's).
# 6  
Old 10-08-2013
Quote:
Originally Posted by sasisken
Thanks to you both but can u explain me what
Code:
` `

exactly does ? Because im confused.
First: the "backquotes" should not be used any more. They are only there for compatibility issues but their usage is strongly discouraged. instead of `command` use
Code:
$(command)

.

Second: command substitution. Suppose you run a command "somecommand" and it produces some output - any output.

Code:
user@host:/some/where> somecommand
this is some output

You can use this output inside your own commands by using the command substitution device ("$( .... )"). The shell will first run the denoted command, replace the command substitution with its output and only then execute the rest of the line. In the example above, consider:

Code:
user@host:/some/where> echo "== $(somecommand) =="

The first step is to run "somecommand" and replace it with its output:

Code:
echo "== this is some output =="

Then, the resulting command is executed, simply displaying the original output decorated with surrounding equal-signs.

This example is not doing something impressive, but in fpmurphy's case it dealt with a possibly varying command output. This line:

Code:
if (( $(stat -c %s file1) >  $(stat -c %s file2) ))

boils down to:

Code:
if (( 5 > 4 ))

or something such, depending on what "stat -c %s <filename>" would yield.

Btw.: you might notice a slight flaw in your programs logic. What would happen if the two files are of absolutely equal size? The solution is left to the reader, but a hint may be in order: "if"-statements can be nested.

I hope this helps.

bakunin
# 7  
Old 10-08-2013
First time i see constantly used the ( for if statements, i'm used to [..

Regard 'if statements'...
[[ condition ]] && echo succeeded || echo failed

Example:
[[ -z $XDG_DESKTOP_DIR ]] && echo "Your desktop is: $XDG_DESKTOP_DIR" || echo "$HOME/.config/user-dirs.dirs not loaded yet."

Which is the same as: (note the '\', that tells the line isnt dont yet, there must be no trailing spaces!)
Code:
[[ -z $XDG_DESKTOP_DIR ]] && \
    echo "Your desktop is: $XDG_DESKTOP_DIR" || \
    echo "$HOME/.config/user-dirs.dirs not loaded yet."

Which i prefer for 'short code' like setting a variable.

The && is appending the following command if previous command returns 0/true.
The || indicates what is done if the previous condition/command failed.

Examples:
Code:
[[ ! 0 -eq $UID ]] && echo "You're not root, and we abort" && exit
[[ 0 -eq $UID ]] || echo "You're not root, but we continue anyway"
[[ 0 -eq $UID ]] && echo "Must not be root!" && exit || echo "You may pass $USER"

hth

Last edited by sea; 10-08-2013 at 11:03 AM.. Reason: uh.. wasnt asked.. but since we're on it..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Getting the process ID of the terminal in Unix/Linux

Hi, How can we get the process id of the terminal we are using? When we logged in to unix, we have an associated terminal. we can use "tty" command to get the terminal we are using like: /dev/pts/0 I want to know the process id of this terminal. Please reply as I searched a lot but I... (8 Replies)
Discussion started by: crazybisu
8 Replies

2. UNIX for Dummies Questions & Answers

Linux terminal server?

Hello everyone, I have an interesting project I'd like to implement on a Linux server here at work. Essentially, I'd like to replace a handful of Windows servers with a single Linux server. The only task these Windows servers perform, is provide remote desktops via RDP protocol that people... (13 Replies)
Discussion started by: lupin..the..3rd
13 Replies

3. UNIX for Dummies Questions & Answers

Is there picture based game under linux terminal?

Is there picture based game under linux terminal? Just like Supermario under DOS. (18 Replies)
Discussion started by: vistastar
18 Replies

4. Homework & Coursework Questions

help with linux terminal window

Hello! I need to create a file and provide access to two users of the file under the same command in linuxs terminal window. The question is how can I do it? (3 Replies)
Discussion started by: Messe
3 Replies

5. Linux

how to perform a system check on linux via terminal

hi im new to this and i just want to learn about linux and i just wanted to know how would i be able to perform a system check to see if a directory exists. can any one help me? (2 Replies)
Discussion started by: roozis
2 Replies

6. Windows & DOS: Issues & Discussions

looking for linux like tab terminal for windows

Hello all is there any free tool like linux tabbed terminal but for windows im used to work with putty and its great but i wander if there something like putty but with tabs thanks (12 Replies)
Discussion started by: umen
12 Replies

7. Ubuntu

Error on my Linux terminal

Hi, I keep on getting the following error on my linux terminal. It did not harm my system so far, but I was wondering if this can be eliminated. { Gecko:4617): GLib-GObject-CRITICAL **: file gobject.c: line 1337 (g_object_unref): assertion `G_IS_OBJECT (object)' failed (Gecko:4617):... (2 Replies)
Discussion started by: gsabarinath
2 Replies

8. Shell Programming and Scripting

How to have color coded Terminal display,(like linux)

Hi all, I would like to know how to have a color display in the terminal... In the sense that, In many linux terminals,we have color coded for each file type, green for executable ,blue for dirs and so on... I wanted to know how i can have the same arrangement in solaris(b-79a) I am not... (5 Replies)
Discussion started by: wrapster
5 Replies

9. Solaris

Terminal settings from linux to solaris

When I log in from my linux workstation (CentOS4) to a solaris 8 server using SSH or telnet, the terminal settings don't seem to work well. When I tail or vi a file, I get a blank screen or no response, and I am no longer able to interact with the session. I have to type the escape sequence to... (2 Replies)
Discussion started by: tjlst15
2 Replies
Login or Register to Ask a Question