Howto make ksh skript bash-compatible with backticks


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Howto make ksh skript bash-compatible with backticks
# 1  
Old 06-19-2009
Howto make ksh skript bash-compatible with backticks

I have the following ksh-script:
Code:
#!/bin/ksh
# Ueberprüfe, ob genau ein Parameter angegeben wurde
test "$#" -eq "1"  || { echo "USAGE: path_cleanup <PATH_NAME>"; return 1; }

# Ueberpruefe, ob awk und nawk installiert sind
test -x /bin/nawk || { echo "ERROR: nawk is not installed"; return 1; }
test -x /usr/xpg4/bin/awk || { echo "ERROR: awk is not installed"; return 1; }

# Schreibe den Inhalt von $1 nach PATH_CONTENT
PATH_CONTENT=$(eval echo \$$1)

# Loesche alle doppelt vorkkomenden Verzeichnisse aus PATH_CONTENT von hinten beginnend
PATH_CONTENT=`echo "$PATH_CONTENT" | /usr/xpg4/bin/awk -v RS=':' -v ORS=":" '!a[$0]++' | /bin/nawk 'NR==1'`

export $1=$PATH_CONTENT

Now I want to make this script bash compatible. Problem is line 10. When I replace it with the following, it returns the PID and not the path content:
Code:
PATH_CONTENT=`eval echo \$$1`

So, what do I have to do, because the $() doesn't work in bash.
# 2  
Old 06-19-2009
What version of bash you're using ($BASH_VERSION)?
Anyway I believe you need something like this:

Code:
eval PATH_CONTENT=\$$1

# 3  
Old 06-19-2009
Hi.
$() works perfectly fine in Bash (at least on AIX and Solaris).

Why have you escaped the $...

PATH_CONTENT=$(eval echo \$$1)

Why use eval at all?

What's wrong with...

PATH_CONTENT=$1

$1=`export $PATH_CONTENT` doesn't work in Bash or KSH

Then, if you want to set the new path with the duplicates removed, perhaps you should remove the line

$1=`export $PATH_CONTENT`
(because it doesn't work)

and run the script as SOME_PATH=$(my_program $SOME_PATH)

Gruss,

Code:
#!/bin/ksh (or #!/bin/bash)
 
# Ueberprüfe, ob genau ein Parameter angegeben wurde
test "$#" -eq "1"  || { echo "USAGE: path_cleanup <PATH_NAME>"; return 1; }
 
# Ueberpruefe, ob awk und nawk installiert sind
#test -x /bin/nawk || { echo "ERROR: nawk is not installed"; return 1; }
#test -x /usr/bin/awk || { echo "ERROR: awk is not installed"; return 1; }
 
AWK=$(which awk 2>/dev/null)
 
[ $? -ne 0 ] && { echo "ERROR: awk is not installed"; return 1; }
 
# Schreibe den Inhalt von $1 nach PATH_CONTENT
PATH_CONTENT=$(eval echo \$1)
 
# Loesche alle doppelt vorkkomenden Verzeichnisse aus PATH_CONTENT von hinten beginnend
PATH_CONTENT=`echo "$PATH_CONTENT" | $AWK -v RS=':' -v ORS=":" '!a[$0]++' | $AWK 'NR==1'`
 
#export $1=$PATH_CONTENT
 
echo $PATH_CONTENT



---------- Post updated at 08:42 AM ---------- Previous update was at 08:10 AM ----------

ein wenig einfacher?

Code:
#!/bin/ksh
 
# Ueberprüfe, ob genau ein Parameter angegeben wurde
test "$#" -eq "1"  || { echo "USAGE: path_cleanup <PATH_NAME>"; return 1; }
 
# Ueberpruefe, ob awk installiert ist
AWK=$(which awk 2>/dev/null) || { echo "ERROR: awk is not installed"; return 1; }
 
# Loesche alle doppelt vorkkomenden Verzeichnisse aus PATH_CONTENT von hinten beginnend
echo "$1" | $AWK -v RS=":" -v ORS=":" '!P[$1]++ { print }' | sed -e "s/:$//"

Code:
SOME_PATH=$(my_program $SOME_PATH)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Complex: bash, using ANSI-C quotes in skript

Hello, I hope someone can hep with this. I use a skript to send multiline Data to a Monitoring system. Bu I'm not able to use linebreaks or escape sequences. The skript is simple like that: #!/bin/bash var="Erste Zeile \n zweite Zeile \n Dritter Teil" zabbix_sender -c... (17 Replies)
Discussion started by: mpmichael
17 Replies

2. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

3. Shell Programming and Scripting

To make my script multi-os compatible

Hi, I would like to make my script multi-os compatible and I am having problems to make it work. I would like it to be compatible with those 4 linux versions : Ubuntu, Debian, Fedora and Centos. I am mostly confused when it comes to the repository installation and the different os... (1 Reply)
Discussion started by: gaaara
1 Replies

4. Shell Programming and Scripting

[solved] using backticks to call bash from perl

Hi all, Here is my code: my $x = `bash -c \" ls -l filename | awk '{print \$5}'\"`; print "$x\n"; This will run the first part of the bash script but not the awk command. It therefore gives output of: -rw-r--r-- 1 root root 13619200 2012-04-25 08:16 filename I am actually trying to... (0 Replies)
Discussion started by: free2rhyme2k
0 Replies

5. Shell Programming and Scripting

making code compatible to previous bash versions

First let me explain the scenario I have tywo files as usual file1.txt (it has n rows and 8 columns) $1 $2 $3 $4 $5 $6 $7 $8 Code: 1234567|iufgt|iuoy|iout|white |black |red |90879 1234567|iufgt|iuoy|iout|green |pink |blue |90879... (3 Replies)
Discussion started by: s.deepak
3 Replies

6. Shell Programming and Scripting

HowTo translate KSH Scripts to DOS Batch Files ?

Hi there, in near future I have to change my work surrounding from HP UNIX to Windows Vista (great to get rid of old hardware :), but bad to loose UNIX :( ). As I heavily use KSH scripts to do my job, I was wondering, if there is any HowTo available, supporting me in re-writing the scripts to... (4 Replies)
Discussion started by: Joe-K7
4 Replies

7. Shell Programming and Scripting

Need help howto make a script for Set SNOOP run for 5 minutes

Hi all, I want to monitoring my interface every 6 hours where i want to run snoop command to capture all packet through the interface, so i want running snoop then snoop will run for 5 minutes after that snoop stop then will start again after 6 hours than run for 5 minutes again. thereis any... (9 Replies)
Discussion started by: tindasz
9 Replies

8. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

9. Gentoo

cpu%/mem% usage, scripting, dzen2: howto learn bash the hard way

I am trying to write a small (and rather simple) script to gather some info about the system and piping it to dzen2 first, i want to explain some things. I know i could have used conky, but my intention was to expand my knowledge of bash, pipes and redirections inside a script, and to have fun... (14 Replies)
Discussion started by: broli
14 Replies

10. UNIX for Dummies Questions & Answers

Howto make an URL world readable

Currently this url: http://bioinfo-z.comp.nus.edu.sg/Test/ Is only accessible through my local network (for external user need VPN). My question is how can I make that URL accessible for everybody even those outside my network? That url is served under Solaris 10 and I do have SU... (3 Replies)
Discussion started by: monkfan
3 Replies
Login or Register to Ask a Question