bash cut to variable and cd - bash

Hi i am having a really strange problem.
I am trying to run the following scrip as root:
loc=$(transmission-remote 192.168.1.74:9091 --auth=user:password -t$TORRENTID -i |sed -e '1d;$d;s/^ *//'|grep Location: |cut -s -d : -f2)
cd "$loc"
But get the error messege :
cd: can't cd to /leprechaun_tv-shows/download/
But i works if i create a new string:
hello= /leprechaun_tv-shows/download/
cd $hello
what am i doing wrong?

Why are you quoting "$loc" ?
If $loc has a leading space, then quoting the variable expansion will pass the directory with that leading space as an argument.
e.g. cding to root in this fashion gives me:
$ cd " /"
cd: no such file or directory: /
Just remove the quoting. e.g.
cd $loc

Alternatively to Brian's solution, if you still need $loc to be quoted in cd "$loc" because the path could contain any spaces, then just use sed 's/^ *//g' to trim any leading spaces in $loc, like this:
loc=$(transmission-remote 192.168.1.74:9091 --auth=user:password -t$TORRENTID -i |sed -e '1d;$d;s/^ *//'|grep Location: |cut -s -d : -f2 | sed 's/^ *//g')
cd "$loc"

Since it appears you have some initial whitespace in $loc, you can trim it with bash's parameter expansion:
loc=$(transmission-remote 192.168.1.74:9091 --auth=user:password -t$TORRENTID -i |sed -e '1d;$d;s/^ *//'|grep Location: |cut -s -d : -f2)
cd "${loc%% }"

Related

How to get sed or awk commands in a variable

I'm trying to change the filename from
prod.test.PVSREGPLUS20170915-6777.DAT.gpg
to
PVSREGPLUS20170915-0003.DAT.gpg
I used this
DTE=$(date +%I);ls prod.test* |cut -f 3,4,5 -d .|sed "s/\-/-00$DTE/" |cut -c 1-23,28-35
My problem is I need this command in a shell script
"#! /bin/bash
DTE=$(date +%I)
newfile=$(ls prod.test* |cut -f 3,4,5 -d .|sed "s/-*./$DTE/"|cut -c 1-23,28-35
The sed can't do expansion, would awk be able to do this? Any help would be greatly appreciated. Thanks
The simplest way to do this is with a for-loop over a glob pattern, then use paramater expansion to remove the prefix
prefix="prod.test."
hour=$(date '+%I')
for f in "$prefix"*; do
new=$( echo "${f#$prefix}" | sed 's/-[[:digit:]]\+/-00'"$hour"'/' )
echo mv "$f" "$new"
done
We really don't need sed: extended patterns and more parameter expansion
shopt -s extglob
for f in "$prefix"*; do
new=${f#$prefix}
new=${new/-+([0-9])/-00$hour}
echo mv "$f" "$new"
done
Remove "echo" if it looks good.
Or, with the perl rename as suggested in the comments:
rename -v -n 's/prod\.test\.//; use Time::Piece; s{-\d+}{"-00" . (localtime)->strftime("%I") }e' prod.test.*
Remove "-n" if it looks good.

cygpath inconsistent with variable as input unix to windows conversion

I have a bash script that prompts a user to drag and drop a file as an input and then I need to convert that path to windows style path so I can use it as an argument to an .exe that is being called:
echo -e "\nPlease drag and drop your input audio file into this window and hit Enter\n"
read inputfile
inputfilewin=$(cygpath -w -a "${inputfile}")
echo "inputfile: $inputfile"
echo "inputfilewin: $inputfilewin"
This works fine for paths with no spaces but my Box Sync folder produces a response like this:
C:\cygwin\home\jimbob\'\cygdrive\c\Users\jimbob\Box Sync\myscripts\testscript\6ch.wav'
but if i just type on the command line cygpath -w -a and drag and drop the same file i get the right path back:
C:\Users\jimbob\Box Sync\myscripts\testscript\6ch.wav
Please help I just can't get it to work how I need
$ echo $a
/cygdrive/c/Program Files/Autodesk/DWG TrueView 2016 - English
so depending on how many escape you need:
$ cygpath -w "$(echo -n $a |sed -e 's/ /\ /g')"
C:\Program Files\Autodesk\DWG TrueView 2016 - English
$ cygpath -w "$(echo -n $a |sed -e 's/ /\\ /g')"
C:\Program\ Files\Autodesk\DWG\ TrueView\ 2016\ -\ English

Creating a file using parameters of other file using UNIX Shell Script

I have a series of files
484_mexico_201401.dat
484_mexico_201402.dat
484_mexico_201403.dat
… so on
I want to make files
484_mexico_201401.mft which will have below containt
484 | datfile name | line count for the .dat file
Example:
484|484_mexico_201401.dat|6000
can anyone help with a shell script for this ?
You can try bash,
for file in 484_*
do
new_file=${file%.*};
echo "$(sed 's/^\([^_]\+\)_.*/\1/'<<<$file)|$file|$(wc -l $file|cut -d' ' -f1)" > "$new_file.mft";
done
you can also try this.
location="./";for file in $(ls $location);do echo "$(echo $file|cut -d '_' -f 1)|$file|$(wc -l $file | cut -d ' ' -f 1)" >> output.txt;done
Then you will be able to read the new file buy typing cat output.txt
If you want to full script for it, then you may need to add the #!/bin/bash to the first line in the script.
#!/bin/bash
location="./";for file in $(ls $location);do echo "$(echo $file|cut -d '_' -f 1)|$file|$(wc -l $file | cut -d ' ' -f 1)" >> output.txt;done
save that into a file where you want the script to be then run chmod 555 scriptname.sh and you should be able to run.

BASH CUT associates with CD

i need some help here as i couldn't figure it out the errors.
read input
echo -e "$input" | cut -c4-
cd "$input" | cut -c4-
So I enter cd test, echo output is correct which is test.
I would like to change directory but it gives cd cd test.
Any help is appreciated.
What you want should be:
cd $(echo -e "$input" | cut -c4-)
Which can be done as well like this (see bash "here strings")
cd $(cut -c4- <<<$input)

Using DOS file contents as command line arguments in BASH

This is a follow-up to this question's answer.
How can I modify the code so that the annoying CRLF of a DOS created file can be stripped away before being passed to xargs?
Example file 'arglist.dos'.
# cat > arglist.unix
src/file1 dst/file1
src/file2 dst/file2
src/file3 dst/file3
^c
# sed 's/$/\r/' arglist.unix > arglist.dos
The unix variant of the file works with this:
$ xargs -n2 < arglist.unix echo cp
cp src/file1 dst/file1
cp src/file2 dst/file2
cp src/file3 dst/file3
For my own education, how can I change it to accept either the 'arglist.unix' or 'arglist.dos' files on the same command line?
cat arglist.dos | tr -d "\r" | xargs -n2 echo cp
gives you the same result as
cat arglist.unix | tr -d "\r" | xargs -n2 echo cp
so it works on both files.
tr -d "\r" removes all the CR characters
Use d2u to remove the CR before passing the file to xargs.

Resources