tristan202
tristan202

Reputation: 1129

Use forward slash in variable

I wrote a script to ease the syncing and building of Android source. I tried adding a function to cherrypick patches, but I can't get it to work properly. I know it's because of the forward slashes, but I don't know how to protect/escape them.

Part of the code is:

   echo "Copy/paste the project folder, i.e. 'frameworks/base'"
   read folder
   echo ""
   echo "Now paste the cherry-pick git link, i.e. 'git fetch <someproject> refs/changes/... && git cherry-pick FETCH_HEAD'"
   read cherry
   echo ""
   Begin
   clear
   echo ""
   export IFS="&&"
   for x in $cherry
   do
        cd ${CM}/${folder}
    CHERRY=$(trim "$x")
    $CHERRY
   done

Let's say that the 'cherry' variable is:

git fetch http://r.cyanogenmod.com/CyanogenMod/android_frameworks_base refs/changes/68/22968/2 && git cherry-pick FETCH_HEAD

I would get this error:

/home/tristan202/bin/build_cm.sh: line 159: git fetch http://r.cyanogenmod.com/CyanogenMod/android_frameworks_base refs/changes/91/23491/2: No such file or directory
/home/tristan202/bin/build_cm.sh: line 159: git cherry-pick FETCH_HEAD: command not found

I cannot figure out why it fails.

The 'trim' function it calls is a function that trims leading and trailing spaces. If I do echo "$CHERRY" within the for loop, the commands are printed correctly, but it still fails.

Upvotes: 1

Views: 2995

Answers (2)

kev
kev

Reputation: 161674

I will give your another example:

cmd='echo hello && echo world'
$cmd

The result is:

hello && echo world

bash parses the command $cmd as Simple Commands not Lists of Commands.
After Parameter Expansion, && is passed as argument to echo(1st word after Word Splitting).


The solution is pulling && out:

cmd1='echo hello'
cmd2='echo world'
$cmd1 && $cmd2

Upvotes: 3

Douglas Leeder
Douglas Leeder

Reputation: 53310

Once you put && in a variable it ceases to be interpreted as separating two commands:

$ A="echo a && echo b"
$ echo $A
echo a && echo b
$ echo c && ${A}
c
a && echo b

So you need to avoid putting && into a variable.

git was even telling you that the && was the problem in its error message.

Upvotes: 0

Related Questions