PDA

View Full Version : Shell Redirection?



huangyingw
February 1st, 2010, 03:20 PM
Hello,
I had following script, named back.sh, as bellow:

#! /bin/bash

lv_file=/root/myproject/git/linux/shell/fundamental/log.txt
lvdisplay | grep -o "/dev.*" > $lv_file

cat $lv_file | while read file ; do
echo "$file" | sed 's/dev/media/' | mkdir -p '{}'
#echo "$file" | sed 's/dev/media/'
#mkdir -p 'echo "$file" | sed 's/dev/media/''
done

I would like the "mkdir -p" command to use the output of "echo "$file" | sed 's/dev/media/'", but it does not work.
BTW, another question:
How to use sed to replace directly from a string, instead of a file.
For example, I know
sed 's/dev/media/' < $file, in which file contains the string I would like to replace.
While, I am expecting a direct form like bellow:
sed 's/dev/media/' $mystring. But I don't know how to realize this.

Sporkman
February 1st, 2010, 03:48 PM
Try:

myval=`echo "$file" | sed 's/dev/media/'`
mkdir -p "$myval"

huangyingw
February 2nd, 2010, 04:18 AM
Try:

myval=`echo "$file" | sed 's/dev/media/'`
mkdir -p "$myval"
yes, thank you. This is indeed a simple and useful solution.
My head is stuck at that time.:(

Sporkman
February 2nd, 2010, 04:37 AM
My head is stuck at that time.:(

That happens to us all. :)

geirha
February 2nd, 2010, 09:26 AM
The shell can also do that substitution itself.


while read -r file; do
mp="/media${file#/dev}"
mkdir -pv "$mp"
done < <(lvdisplay | grep -o "/dev.*")

huangyingw
February 3rd, 2010, 03:13 PM
The shell can also do that substitution itself.


while read -r file; do
mp="/media${file#/dev}"
mkdir -pv "$mp"
done < <(lvdisplay | grep -o "/dev.*")

Great, thank you.