LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs
User Name
Password

Notices


Old

Getting Absolute Paths of UNIX Directories and Filenames in Shell Scripts

Posted 07-08-2011 at 03:05 AM by konsolebox
Updated 05-27-2018 at 03:20 PM by konsolebox
Tags bash, ksh, script, shell, zsh

This post is a compilation of the function getabspath() for different shells that can be used to get the absolute form of a path. The function accepts an argument path then returns the absolute form of it to the variable $__.

Bash
Code:
function getabspath {
	local t=() i=0 IFS=/

	case $1 in
	/*)
		__=${1#/}
		;;
	*)
		__=${PWD#/}/$1
		;;
	esac

	case $- in
	*f*)
		set -- $__
...
Senior Member
Posted in Howtos
Views 4304 Comments 0 konsolebox is offline
Old

If condition "if condition ... if condition"

Posted 08-28-2010 at 06:26 PM by the dsc (linux-related notes)
Updated 06-01-2023 at 12:47 PM by the dsc

If you want to have a script that would do something if either one of two consequences is true, a simple way to do that would be:

Code:
if [ condition 1 ] || [ condition 2 ] ; then
if [ condition 1 ] ; then
consequences for condition 1
fi
if [ condition 2 ] ; then
consequences for condition 2
fi
consequences of either one, but happens only once, not twice
fi
...
You check if either one is true, when "||"...
Member
Posted in Uncategorized
Views 1556 Comments 0 the dsc is offline
Old

Shortest command to calculate the sum of a column of output

Posted 06-02-2010 at 06:27 AM by bittner (Notes on Linux)

I had to calculate the Installed-Size for a .deb control file recently. Here is the solution I came up with:

Code:
dpkg-deb -c mypackage.deb | sed -e 's/.*root\s*//' -e 's/\s*2010-.*$//' | xargs | tr ' ' + | bc
What it does? The code tries to construct a calculation string and feeds it to the command line calculator bc as follows:
  1. use dpkg-deb to get the file size information out of the Debian package
  2. sed away all characters before and after the
...
LQ Newbie
Posted in Uncategorized
Views 7970 Comments 2 bittner is offline
Old

having fun with bash varaibles: indirect variable references

Posted 11-28-2009 at 01:38 PM by lumak (Lumak's Guide to Random Things)
Updated 11-28-2009 at 02:59 PM by lumak

Well I recently started messing around with bash scripts as cgi scripts and was wondering how to pass variables in and what not.

Well it turns out, Apache will define the variable $QUERY_STRING which will contain everything after the first question mark. You can then pars this string to define variables. The general format is "name1=val1&name2=val2". It turns out you can actually declare variables in bash using 'declare' and parameter expansion.

...
Attached Files
File Type: txt cgi_vars.txt (2.4 KB, 529 views)
Member
Views 15019 Comments 2 lumak is offline
Old

If - how to use

Posted 12-05-2008 at 02:27 AM by kshfi (Jukka's script corner)
Updated 12-07-2008 at 09:57 AM by kshfi
Tags bash, ksh

Programmers - shell if is command.

Code:
if command exit code is 0
then
    do something 1
    do something 2
fi
Ex.
Code:
if  cp fromfile tofile
then
   echo "Works fine"
else
   echo "Error ???"
fi
[ = test
You can write test or [

Code:
a=1
b=1
test "$a" = "$b"
echo $?
0

a=1
b=2
...
LQ Newbie
Views 1904 Comments 0 kshfi is offline

  



All times are GMT -5. The time now is 09:11 AM.

Main Menu
Advertisement
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